Click here to Skip to main content
11,645,893 members (75,562 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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:
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 13-Feb-13 3:54am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Problem solved Smile | :) . 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:
Stream = New IO.FileStream(FilePath, _
                           IO.FileMode.Create, _
                           IO.FileAccess.Write)
Writer = New IO.StreamWriter(Stream)
 
Writer.Write(Space(30000))
Writer.Close()
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 523
1 jyo.net 514
2 Sergey Alexandrovich Kryukov 503
3 Afzaal Ahmad Zeeshan 368
4 DamithSL 305
0 OriginalGriff 1,143
1 jyo.net 994
2 Sergey Alexandrovich Kryukov 808
3 Afzaal Ahmad Zeeshan 702
4 DamithSL 691


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100