【问题标题】:How do I output everything my program prints to a file?如何将程序打印的所有内容输出到文件中?
【发布时间】:2014-04-07 19:34:07
【问题描述】:

问题是,我编写了一个代码来生成巨大的恒星金字塔。现在我想将控制台中写入的所有内容输入到文本文件中。 谢谢。

我当前的代码。

using System;

namespace Pyramidi
{
class Ohjelma
{
    static void Main()
    {
        int maxHeight = 0;
        do
        {

            Console.Write("Anna korkeus: ");
            maxHeight = Convert.ToInt32(Console.ReadLine());
            if (maxHeight > 0)
            {
                break;
            }
            else
            {
                continue;
            }
        }
        while (true);

        for (int height = 0; height < maxHeight; height++)
        {
            for (int i = 0; i < (maxHeight - height - 1); i++)
            {
                Console.Write(" ");
            }
            for (int i = 1; i <= (height * 2 + 1); i++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}

}

【问题讨论】:

  • 喜欢myProgram.exe &gt; someFile.txt 或者您想要更改代码以写入文件或者您想要tee 等效于Windows 或其他?

标签: c# output


【解决方案1】:

最简单的方法是在运行时将程序的输出重定向到文件:

MyProject.exe > file.txt

">" 是一个“重定向运算符”,就像在许多 Unix shell 中一样。

如果您使用的是 Windows,您可以阅读有关输出重定向运算符和其他此类运算符 here 的更多信息。如果您使用的是 Unix shell,请使用您的 shell 的重定向运算符(例如,here's the Bash manual's advice)。

【讨论】:

  • 你是如何做到这一点的?
  • @Tatu:在命令行运行程序时,只需在其后添加“...> file.txt”,正如答案中所示。
  • +1。开始 -> 运行 -> "cmd"...cd /d 到您的可执行文件的文件夹...键入程序名称并重定向到文件...
  • +1 这是我实际要做的,而不是我的答案。 Shell 命令 FTW
【解决方案2】:

John 的回答是最快最简单的,但 StreamWriter 也是一种解决方案。这是您需要写入文件时经常使用的东西。

我建议阅读一下StreamWriter。这允许您输出到文件。

您只需要在其中添加一个 StreamWriter 对象并将 Console.WriteLines 替换为 StreamWriter 变量名称。

using (StreamWriter sw = new StreamWriter("fileName.txt"))
{
    for (int height = 0; height < maxHeight; height++)
    {                   
        for (int i = 0; i < (maxHeight - height - 1); i++)
        {
            sw.Write(" ");
        }
        for (int i = 1; i <= (height * 2 + 1); i++)
        {
            sw.Write("*");
        }
        sw.WriteLine();
    }
    sw.Flush();
}

【讨论】:

    【解决方案3】:

    这是使用Console.SetOut方法的另一种解决方案:

    using (var writer = new StreamWriter("filepath"))
    {
       Console.SetOut(writer);
       do
       {
           Console.Write("Anna korkeus: ");
           maxHeight = Convert.ToInt32(Console.ReadLine());
    
           if (maxHeight > 0)  break;
           else continue;
      }
      while (true);
    
      for (int height = 0; height < maxHeight; height++)
      {
           for (int i = 0; i < (maxHeight - height - 1); i++)
           {
               Console.Write(" ");
           }
           for (int i = 1; i <= (height * 2 + 1); i++)
           {
                Console.Write("*");
           }
           Console.WriteLine();
      }
      writer.Flush();
    }
    

    Console.SetOut 更改了Console 的输出流。因此,当您使用Console.Write 时,它会写入该流而不是Console。然后您调用Flush 方法将所有数据写入底层流并清除缓冲区。

    【讨论】:

      【解决方案4】:

      不用写到Console,直接用IO写到文件

      using System;
      using System.IO;
      
      class Test
      {
          public static void Main()
          {
              string path = @"c:\temp\MyTest.txt";
              if (!File.Exists(path))
              {
                  // Create a file to write to. 
                  using (StreamWriter sw = File.CreateText(path))
                  {
                      sw.WriteLine("Hello");
                      sw.WriteLine("And");
                      sw.WriteLine("Welcome");
                  }
              }
      
              // Open the file to read from. 
              using (StreamReader sr = File.OpenText(path))
              {
                  string s = "";
                  while ((s = sr.ReadLine()) != null)
                  {
                      Console.WriteLine(s);
                  }
              }
          }
      }
      

      欲了解更多信息,请访问http://msdn.microsoft.com/en-us/library/system.io.file.aspx

      【讨论】:

      • 问题是针对 C#的
      • 这个问题与VB无关。
      • 如果要复制粘贴,至少要从正确的语言复制粘贴:)
      • 我也想知道,不过感谢您的快速建议。可惜语言不对。 :D
      • 对不起,我在 MSDN 上默认使用 VB。 :P
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 2014-08-01
      相关资源
      最近更新 更多