Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there.

While coding in an application that continuosly logs strings to a text file, I noticed that at some point, the IO.StreamWriter class will simply stop writing, even if it's in the middle of a line. Investigating further, I found out that this also happens when writing one long string at once, as this example code does:
VB
Stream = New IO.FileStream(FilePath, _
                           IO.FileMode.Create, _
                           IO.FileAccess.Write)
Writer = New IO.StreamWriter(Stream)

Writer.Write(Space(30000))

If I open the text file afterwards, instead of the expected 30000 spaces, I find that it contains only 29696. Interestingly, no matter how many characters I try to write, it always seems to be a multiple of 1024 that actually is written.

Does somebody have an idea what the issue could be?
Posted

1 solution

Problem solved :). Instead of closing the stream after the string is written, the writer has to be closed. Otherwise, the writer will only write the size of its buffer that apparently is 1024.

It works fine this way:
VB
Stream = New IO.FileStream(FilePath, _
                           IO.FileMode.Create, _
                           IO.FileAccess.Write)
Writer = New IO.StreamWriter(Stream)
 
Writer.Write(Space(30000))
Writer.Close()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900