File write is not successful after 950ko
<p>i just got back to c# to create a csv to sql translator for one of my project.
However i came accross a weird behaviour and my google search was not successful, so i need your help.</p>

<p>Here is the following code ( tiddied for the occasion, and don't my the poor quality, i'm no csharp expert)</p>

<pre><code>string line, generated;
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('|');
            generated = "INSERT INTO `translation` VALUES('" + ar[0] + "'," + ar[1] + ");";
            w.WriteLine(generated);
        }
// Closing file stuff
</code></pre>

<p>The file is successfully created and greatly generated for small files. However when i use it on large file the resulting file is truncated (always at the same place and with a resulting file of 950ko~)</p>

<p>The result is like this</p>

<pre><code>INSERT INTO `translation` VALUES ( 'trans1', 1 );
INSERT INTO `translation` VALUES ( 'trans2', 2 );
INSERT INTO `translation` VALUES ( 'trans3', 3 );
INSERT INTO `tran
</code></pre>

<p>I miss 6 lines.
So if anyone got an idea i'll gladely listen to eagerly</p>

<p>PS: I could easily solve this by chunking my source file, however I'm really curious about this issue and can't think it is a .Net issue, i must have done something wrong.</p>

<p>Regards all</p>

<p>The closing stuff</p>

<pre><code>w.Close();
file.Close();
fs.Close();
</code></pre>

<p>Edit: solution
Following people recommendation the solution to my problem was the following</p>

<pre><code>// Replacing my FileStream and StreamWriter by the following line
System.IO.StreamWriter w = new System.IO.StreamWriter("data.sql", false, System.Text.Encoding.UTF8);
// Removing the fs.Close()
</code></pre>

<p>It now work like a charm. I could be cleaner by using the 'using' keyword to avoid calling the Close function ( like Mehrdad said ) but i don't like to add an extra set of { } ( my taste )</p>

<p>Thank you all</p>
