c# - File write is not successful after 950ko -


I went back to C # to create a CSV for SQL translator for one of my project though I did a strange behavior Came across and my Google search was not successful, so I need your help.

Here is the following code (tiddied for this occasion, and do not do my poor quality, M not csharp expert) prefix ; System.IO.FileStream fs = New System.IO.FileStream ("data.sql", System.IO.FileMode.Create); System.IO.StreamWriter w = New system. IO.StreamWriter (FS, encoding. UTF8); System.IO.StreamReader file = new system.io.streamreader ("source.txt", System.Text.Encoding.UTF8); While ((line = file.readline ())! = Null ({string [] AR = line Split ('|'); Generate in the generated = "translation" values ​​('"[[[0] +"', "+ ar [1] +"); "; W.WriteLine (generated); } // Close the file content

The file has been successfully created and has generated much more for smaller files. However, when I use it on a larger file, the resulting file is cut (always in the same place and the resultant file is 950 ~)

the result is like this

  Insert into 'translation' values ​​('trans 1', 1); Insert into `translation` values ​​('trans 2', 2); Insert in 'translation' values ​​('trans 3', 3); INSERT IN TRAN   

I miss 6 lines. So if anyone gets an idea then I eagerly listen.

PS: I can easily solve it by pressing my source file, though I am really curious about this issue and do not think May it be one. Net issue, I must have done something wrong.

All Promise

Closing Accessories

  Close W (); File.Close (); Fs.Close ();   

Edit: Solution The following people recommended the solution to my problem was following

  // replace your file stream and streamwriter the following line System.IO by. StreamWriter w = New system. IO.StreamWriter ("data.sql", incorrect, System.Text.Encoding.UTF8); // close fs.Close ()   

It now works like a magic I used the 'clonor' function (using the quotient) to make the closure function (said Mehrdad) Calling can be cleaned by calling, but I do not like to add an extra set of {} (my taste)

Thank you to all

The solution to my question is:

  string line generated; (System.IO.StreamReader file = New system. Using the IO.StreamRedder ("source.txt", System.Text.Encoding.UTF8)) ({System.IO.StreamWriter w = new System.IO.StreamWriter (" Data .sql ", false, System.Text.Encoding.UTF8)} {while ((line = file. Readline (=)) = zero) {string [] AR = line. Split ('|'); Generate in the generated = "translation" values ​​('"[[[0] +"', "+ ar [1] +"); "; W.WriteLine (generated); }}}    

Comments