Question

The thing is, I have made a code to generate giant pyramids of stars. Now I would like to input everything what is written in the console into a text file. Thanks.

My current code.

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();
    }
}

}

Was it helpful?

Solution

The easiest thing to do, and which doesn't require changing your program at all, is to redirect your program's output to a file when you run it:

MyProject.exe > file.txt

">" is a "redirection operator", as it is in many Unix shells.

If you're on Windows, you can read more about the output redirection operator and other such operators here. If you're using a Unix shell, use your shell's redirection operators (e.g., here's the Bash manual's advice).

OTHER TIPS

John's answer is the fastest and easiest, but StreamWriter is also a solution. This is something that you will use often when you need to write to a file.

I would suggest reading up about StreamWriter. This allows you to do output to a file.

You just need to add a StreamWriter object in and replace the Console.WriteLines with the StreamWriter variable name.

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();
}

Here is another solution using Console.SetOut method:

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 changes the output stream of the Console.So when you use Console.Write it writes to that stream instead of Console.And then you call Flush method which is writes all the data in the underlying stream and clears the buffer.

Instead of writing to the Console, write directly to the file using 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);
            }
        }
    }
}

For more information, visit this http://msdn.microsoft.com/en-us/library/system.io.file.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top