Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
AnswerRe: New line with streamwriter Pin
Luc Pattyn16-Mar-11 6:29
sitebuilderLuc Pattyn16-Mar-11 6:29 
AnswerRe: New line with streamwriter Pin
Groulien16-Mar-11 5:17
Groulien16-Mar-11 5:17 
GeneralRe: New line with streamwriter Pin
Pierre besquent16-Mar-11 6:03
Pierre besquent16-Mar-11 6:03 
AnswerRe: New line with streamwriter Pin
Paladin200016-Mar-11 6:31
Paladin200016-Mar-11 6:31 
GeneralRe: New line with streamwriter Pin
Luc Pattyn16-Mar-11 7:22
sitebuilderLuc Pattyn16-Mar-11 7:22 
GeneralRe: New line with streamwriter Pin
Paladin200016-Mar-11 8:26
Paladin200016-Mar-11 8:26 
GeneralRe: New line with streamwriter Pin
Luc Pattyn16-Mar-11 8:33
sitebuilderLuc Pattyn16-Mar-11 8:33 
GeneralRe: New line with streamwriter Pin
Paladin200016-Mar-11 8:45
Paladin200016-Mar-11 8:45 
Data can end up in the file, even if it is not closed. When the StreamWriter buffer fills (default: 4KB), that portion is written to file.

To modify your earlier example (omitting the "using" statement, thereby denying a close):

string fn = "swl.txt";
StreamWriter sw = new StreamWriter(fn, true, Encoding.ASCII);
sw.WriteLine("line 1");
sw.WriteLine("line 2");
sw.WriteLine();
sw.WriteLine();
sw.WriteLine("line 5");
sw.WriteLine("line 6");
sw.WriteLine();
sw.WriteLine();


Nothing is written to the file. However, when I use:

string fn = "swl.txt";
StreamWriter sw = new StreamWriter(fn, true, Encoding.ASCII);
sw.WriteLine("line 1");
sw.WriteLine("line 2");
sw.WriteLine();
sw.WriteLine();
sw.WriteLine("line 5");
sw.WriteLine("line 6");
for (int i = 0; i < 5000; i++) sw.Write("X");  //Used to fill up the buffer and thereby write to file
sw.WriteLine();
sw.WriteLine();
sw.Write("Will not go in file.");


I have exceeded the buffer, and the first 4K writes. When I run this code, I get a file with the lines up to the "for" statement, plus many "X" characters, but none of the later lines are "written". The file is readable, just incomplete after the program closes.
GeneralRe: New line with streamwriter Pin
Luc Pattyn16-Mar-11 9:46
sitebuilderLuc Pattyn16-Mar-11 9:46 
GeneralRe: New line with streamwriter Pin
Pierre besquent17-Mar-11 0:24
Pierre besquent17-Mar-11 0:24 
AnswerRe: New line with streamwriter Pin
Luc Pattyn17-Mar-11 4:35
sitebuilderLuc Pattyn17-Mar-11 4:35 
GeneralRe: New line with streamwriter Pin
Pierre besquent17-Mar-11 6:00
Pierre besquent17-Mar-11 6:00 
GeneralRe: New line with streamwriter Pin
Luc Pattyn17-Mar-11 6:29
sitebuilderLuc Pattyn17-Mar-11 6:29 
AnswerRe: New line with streamwriter Pin
jschell16-Mar-11 8:27
jschell16-Mar-11 8:27 
GeneralRe: New line with streamwriter Pin
Pierre besquent17-Mar-11 0:15
Pierre besquent17-Mar-11 0:15 
GeneralRe: New line with streamwriter Pin
jschell17-Mar-11 10:03
jschell17-Mar-11 10:03 
GeneralRe: New line with streamwriter Pin
Pierre besquent17-Mar-11 23:11
Pierre besquent17-Mar-11 23:11 
AnswerRe: New line with streamwriter Pin
Pete O'Hanlon16-Mar-11 8:47
mvePete O'Hanlon16-Mar-11 8:47 
QuestionC# webpage playing a video from server hard drive Pin
twinscythe1233216-Mar-11 2:47
twinscythe1233216-Mar-11 2:47 
AnswerRe: C# webpage playing a video from server hard drive Pin
twinscythe1233216-Mar-11 3:15
twinscythe1233216-Mar-11 3:15 
GeneralRe: C# webpage playing a video from server hard drive Pin
Dan Mos16-Mar-11 5:47
Dan Mos16-Mar-11 5:47 
GeneralRe: C# webpage playing a video from server hard drive Pin
twinscythe1233217-Mar-11 0:59
twinscythe1233217-Mar-11 0:59 
QuestionStreamwriter output Pin
Pierre besquent15-Mar-11 21:53
Pierre besquent15-Mar-11 21:53 
AnswerRe: Streamwriter output Pin
Mycroft Holmes15-Mar-11 22:18
professionalMycroft Holmes15-Mar-11 22:18 
GeneralRe: Streamwriter output Pin
Pierre besquent15-Mar-11 22:51
Pierre besquent15-Mar-11 22:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.