StreamWriter and NewLine
- hallo everybody,
I want to create a text file by writing a single line at a time and use the following method.
FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None); StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default); sw.WriteLine("Line 1"); sw.Close();
Problem is that the text file contains two lines where the second line is empty. StreamWriter advances to next line after writing a line is the reason i think....
How can i create the text file without an empty line at the end of the file. ?
I don't want to use StringBuilder or any thing like that to create the text and write the whole content all together to the text file.
How can i do it ? I am using dot net 2.0
Thanks for any help in advance :-)
regards
swingme
Réponses
- The last time you write to the file, use Write instead of WriteLine.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser
Toutes les réponses
- The last time you write to the file, use Write instead of WriteLine.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser - WriteLine Writes a line terminator after string. use Write method as David suggests.
- Hallo David,
thanks for ur reply. that means i have to check every time whether i am writing the line last line to the file..is'nt it?
regards
swingme - Somehow, yes.If you're in a loop and you have an array, here's an example:for (int i = 0; i < array.Length - 1; i++) // note the minus 1writer.WriteLine(array[i]);writer.Write(array[array.Length - 1]);
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser - thank you david....
got it !
not in a loop, but u made my eyes open :-)
regards
swingme

