4

Really new to this, I've read the answers on here and they have helped so far but I am still a bit stuck.

I have managed to create a program that displays the users starsign from DOB input.

Now I want to send the results to a text file. I can create the file but I do not know how to send the console output/result of the program to the file.

Rotem
  • 21,452
  • 6
  • 62
  • 109
user1937185
  • 93
  • 2
  • 2
  • 8
  • Welcome to SO. Possible duplicates: http://stackoverflow.com/questions/61110/how-to-save-the-output-of-a-console-application?rq=1 http://stackoverflow.com/questions/420429/mirroring-console-output-to-a-file?rq=1 – Rotem Jan 03 '13 at 17:15

2 Answers2

7
Console.WriteLine("Hello World");
FileStream fs = new FileStream("Test.txt", FileMode.Create);
// First, save the standard output.
TextWriter tmp = Console.Out;
StreamWriter sw = new StreamWriter(fs);
Console.SetOut(sw);
Console.WriteLine("Hello file");
Console.SetOut(tmp);
Console.WriteLine("Hello World");
sw.Close();

http://msdn.microsoft.com/en-us/library/system.console.setout.aspx

trydis
  • 3,905
  • 1
  • 26
  • 31
  • 3
    Technically correct but doubling up every write operation will get quite unwieldy quickly. A more manageable approach would be to either build up a a large string for output at the end & then write it out twice or write a class that multiplexes output to multiple streams. – Sean McSomething Jan 03 '13 at 17:56
1

If you have calculated the date of birth and written it to the console, you have the answer stored in a variable, then all you need to do is write that out to the text file - unless I am missing some in the problem?

http://msdn.microsoft.com/en-us/library/aa287548(v=vs.71).aspx

wyerarch
  • 73
  • 7