Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streamwriter is cutting off my last couple of lines sometimes in the middle of a line?

Here is my code. :

FileStream fileStreamRead = new FileStream(pathAndFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
FileStream fileStreamWrite = new FileStream(reProcessedFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

                StreamWriter sw = new StreamWriter(fileStreamWrite);

                int readIndex = 0;
                using (StreamReader sr = new StreamReader(fileStreamRead))
                {
                    while (!sr.EndOfStream) {
                        Console.WriteLine("eof" + sr.EndOfStream);
                        readIndex++;
                        Console.WriteLine(readIndex);
                        string currentRecord = "";
                        currentRecord = sr.ReadLine();
                        if (currentRecord.Trim() != "")
                        {
                            Console.WriteLine("Writing " + readIndex);
                            sw.WriteLine(currentRecord);
                        }
                        else {
                            Console.WriteLine("*******************************************spaces ***********************");
                        }
                    }

It is cutting off 2 lines with one test file and half a line, and then 1 line and half a line with the other test file I am running it against.

I am not a streamreader/writer expert you can probably see.

Any ideas or suggestions would be greatly appreciated as this is driving me batty. I am sure it is me using these incorrectly.

like image 935
Bill Blankenship Avatar asked Oct 04 '12 21:10

Bill Blankenship


4 Answers

You are missing Flush/Close or simply using for your writer.

using(FileStream fileStreamWrite = 
  new FileStream(reProcessedFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
{
  using(StreamWriter sw = new StreamWriter(fileStreamWrite))
  {
   // .... write everything here
  }
}
like image 107
Alexei Levenkov Avatar answered Oct 12 '22 11:10

Alexei Levenkov


Right after the closing brace of the using statement, do this:

sw.Flush();
sw.Close();

There, that should do it.

like image 26
code4life Avatar answered Oct 12 '22 10:10

code4life


You need to Flush your StreamWriter. A StreamWriter has a buffer, and it writes to disk only when the buffer is full. By flushing at the end you make sure all the text in the buffer is written to the disk.

like image 28
zmbq Avatar answered Oct 12 '22 11:10

zmbq


In addition to other answers (use using, and/or flush/close), would say that they do not actually respond to the question: "why it may cut several lines."

I have an idea on subject that it is related to a fact that you use StreamReader and call EndOfStream twice: in a while loop header, and another inside it.

The only possible way of understanding if the stream ends is try to read some data from it. So I suspect EnfOfStream does it, and reading it twice, may create a problem in stream processing.

To resolve an issue:

  • Or use simple TextReader, considering that you are reading text file (seems to me)

  • Or change your logic to call only once, so no more call to Console.WriteLine("eof" + sr.EndOfStream);

  • Or change your logic, so do not use EndOFStream at all, but read line by line till the line is null.

like image 20
Tigran Avatar answered Oct 12 '22 11:10

Tigran