Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.StreamWriter doesn't write for entire for loop

I am trying to write a long list of numbers to a file in C# but it keeps stopping before the end of the list. For example the following code:

  System.IO.StreamWriter file = new System.IO.StreamWriter("D:\\test.txt");

        for (int i = 0; i < 500; i++)
        {
            file.WriteLine(i);
        }

Leaves me with a text file listing the numbers 0 to 431. Changing 500 to 1000 gets me 0 to 840. It just always seems to stop writing before the loop is finished. Outputing the numbers to console as well as file successfully gives the full list in the console, but not the file.

like image 353
user2851700 Avatar asked Nov 30 '22 12:11

user2851700


2 Answers

You need to close the writer before exiting your program, to make sure that all the buffered output gets written to the file.

A one very convenient way of doing it is with the using statement, which ensures that the StreamWriter gets closed once your loop is done with it:

using (System.IO.StreamWriter file = new System.IO.StreamWriter("D:\\test.txt"))
{
    for (int i = 0; i < 500; i++)
    {
        file.WriteLine(i);
    }
}
like image 194
Sergey Kalinichenko Avatar answered Dec 02 '22 02:12

Sergey Kalinichenko


You discovered that StreamWriter uses a buffer for output, it is 4096 bytes long. This buffer helps making it more efficient, cutting down on the number of calls it makes to the operating system's WriteFile() function. Ensuring that the buffer content makes it to the file requires calling the Flush() method or closing the stream with Close() or Dispose().

If you are sure that you want to keep the file opened then you can add this line of code to ensure that you can see the output when it is written:

    file.AutoFlush = true;

The AutoFlush property is by default false for files. It is true for the Console which is why you can see the output of Console.Write/Line() immediately. Also one of the reasons why console output is so slow.

But given that your file variable is a local variable, you almost surely want to just close the file. Use the using statement to ensure that's taken care of by the time the method that contains this code returns. Forgetting to do this leaves the file opened for a while, at least until the next garbage collection.

like image 42
Hans Passant Avatar answered Dec 02 '22 00:12

Hans Passant