none
C# Code not writing to a file RRS feed

  • Question

  • What is wrong in the code below? It prints correctly on console but does no writing to the file. Thanks guys. This is frustrating me so please help.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace testersss
    {
           class Program
           {
                  static void Main(string[] args)
                  {
                         string outfileName = @"Out.csv";
                         StreamWriter outStream = new StreamWriter(outfileName);
    
                         for (int p = 0; p < 5; ++p)
                         {
                               for (int q = 0; q < 5; ++q)
                               {
                                      Console.Write("," + p);
                                      outStream.WriteLine("," + q);
                               }
                               Console.WriteLine(",");
                               outStream.WriteLine(",");
                         }
    
                  }
           }
    }
    


    • Edited by NewBee13 Friday, August 10, 2012 8:45 PM
    Friday, August 10, 2012 8:41 PM

Answers

  • explicitly closing the stream upon completion might do the trick (or use a 'using' block for an automatic stream closure).

    Friday, August 10, 2012 8:54 PM
  • You aren't closing the stream, or flushing it.  The stream is internally buffering it's content (writing it to a temporary variable and then actually writing it to a file every once in a while).  Streams should always be disposed both because it prevents this problem (leaving data in the buffer when you're done) and also because streams tend to hold onto expensive system resources that should be gotten rid of.

    The very simple solution is to just add a class to outStream.Close or outStream.Dispose after the outer for loop.  (One method just calls the other, so it doesn't matter which you choose.)

    The better approach, which is less fragile, is to use a `using` block.

    string outfileName = @"Out.csv";
    using (StreamWriter outStream = new StreamWriter(outfileName))
    {
    
        for (int p = 0; p < 5; ++p)
        {
            for (int q = 0; q < 5; ++q)
            {
                Console.Write("," + p);
                outStream.WriteLine("," + q);
            }
            Console.WriteLine(",");
            outStream.WriteLine(",");
        }
    }

    This automatically wraps the code inside of the using block in a 'try' clause, and calls 'Dispose' in a finally block afterwards.  This could instead be written out by hand, but using 'using' is quicker and easier.  The advantage of the try/finally is that even if there is an exception in your code the resource still gets disposed (which is often important).  The code it converts to looks like:

    {
        StreamWriter outStream = new StreamWriter(outfileName);
        try
        {
            for (int p = 0; p < 5; ++p)
            {
                for (int q = 0; q < 5; ++q)
                {
                    Console.Write("," + p);
                    outStream.WriteLine("," + q);
                }
                Console.WriteLine(",");
                outStream.WriteLine(",");
            }
        }
        finally
        {
            outStream.Dispose();
        }
    }

    Friday, August 10, 2012 8:55 PM
  • Hello,

    you need to call flush method at the end.

    outStream.Flush()

    HTH


    If this post is helpful, please vote as helpful. If this answers your question, please Mark as the Answer.


    Friday, August 10, 2012 8:58 PM

All replies

  • explicitly closing the stream upon completion might do the trick (or use a 'using' block for an automatic stream closure).

    Friday, August 10, 2012 8:54 PM
  • You aren't closing the stream, or flushing it.  The stream is internally buffering it's content (writing it to a temporary variable and then actually writing it to a file every once in a while).  Streams should always be disposed both because it prevents this problem (leaving data in the buffer when you're done) and also because streams tend to hold onto expensive system resources that should be gotten rid of.

    The very simple solution is to just add a class to outStream.Close or outStream.Dispose after the outer for loop.  (One method just calls the other, so it doesn't matter which you choose.)

    The better approach, which is less fragile, is to use a `using` block.

    string outfileName = @"Out.csv";
    using (StreamWriter outStream = new StreamWriter(outfileName))
    {
    
        for (int p = 0; p < 5; ++p)
        {
            for (int q = 0; q < 5; ++q)
            {
                Console.Write("," + p);
                outStream.WriteLine("," + q);
            }
            Console.WriteLine(",");
            outStream.WriteLine(",");
        }
    }

    This automatically wraps the code inside of the using block in a 'try' clause, and calls 'Dispose' in a finally block afterwards.  This could instead be written out by hand, but using 'using' is quicker and easier.  The advantage of the try/finally is that even if there is an exception in your code the resource still gets disposed (which is often important).  The code it converts to looks like:

    {
        StreamWriter outStream = new StreamWriter(outfileName);
        try
        {
            for (int p = 0; p < 5; ++p)
            {
                for (int q = 0; q < 5; ++q)
                {
                    Console.Write("," + p);
                    outStream.WriteLine("," + q);
                }
                Console.WriteLine(",");
                outStream.WriteLine(",");
            }
        }
        finally
        {
            outStream.Dispose();
        }
    }

    Friday, August 10, 2012 8:55 PM
  • using (StreamWriter outStream = new StreamWriter(outfileName))
                {
                    for (int p = 0; p < 5; ++p)
                    {
                        for (int q = 0; q < 5; ++q)
                        {
                            outStream.WriteLine("," + q);
                        }
                        outStream.WriteLine(",");
                    }
                }

    The stream must be closed.
    Friday, August 10, 2012 8:56 PM
  • Hello,

    you need to call flush method at the end.

    outStream.Flush()

    HTH


    If this post is helpful, please vote as helpful. If this answers your question, please Mark as the Answer.


    Friday, August 10, 2012 8:58 PM