Search code examples
c#listcsvexportstreamreader

Trying to export a nested list of strings to a text or csv file c#


I am trying to export the strings in a nested list to a txt or csv file of the users choice and everything seems to be working but when I actually go to check the file after I have exported it the file is absolutely blank. I went and did it on a separate test program to mock my problem and it worked on that program but when I moved the code over it would still not export anything.

This is just my initialized nested list in case its needed.

List<List<string>> aQuestion = new List<List<string>>();

This is the problem area for the code.

static void writeCSV(List<List<string>> aQuestion, List<char> aAnswer)
    {
        StreamWriter fOut = null;
        string fileName = "";

        //export questions
        //determine if the file can be found
        try
        {
            Console.Write("Enter the file path for where you would like to export the exam to: ");
            fileName = Console.ReadLine();
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException();
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("File {0} cannot be found", fileName);
        }

        //writes to the file
        try
        {
            fOut = new StreamWriter(fileName, false);
            //accesses the nested lists
            foreach (var line in aQuestion)
            {
                foreach (var value in line)
                {
                    fOut.WriteLine(string.Join("\n", value));
                }
            }
            Console.WriteLine("File {0} successfully written", fileName);
        }
        catch (IOException ioe)
        {
            Console.WriteLine("File {0} cannot be written {1}", fileName, ioe.Message);
        }

So if any of you guys can help me with this problem that would be great because it seems like such a small problem but I can't figure it out for the life of me.


Solution

  • It may happen that the buffer was not flushed to the disk. You should dispose the stream writer and it will push everything out to disk:

    using (StreamWriter writer = new StreamWriter(fileName, false)) // <-- this is the change
    {
        //accesses the nested lists
        foreach (var line in aQuestion)
        {
            foreach (var value in line)
            {
                writer.WriteLine(string.Join("\n", value));
            }
        }
    }
    

    On a more elaborate level, streams that may cause performance loss are normally buffered. File streams are definitely buffered, because it would be very inefficient to push each separate piece of data to the IO immediately.

    When you're working with file streams, you can flush their content explicitly using the StreamWriter.Flush() method - that is useful if you want to debug code and wish to see how far it has gone writing the data.

    However, you normally do not flush the stream yourself but just let its internal mechanisms choose the best moment to do that. Instead, you make sure to dispose the stream object, and that will force buffer to be flushed before closing the stream.