Skip to content

Commit 01cefbe

Browse files
authored
Fix snippet buffered read logic (#8560)
The snippet generates a file with 215 bytes. It reads the file with a 1024 byte buffer. While this works with a new 0-initialized buffer, the buffered reading misses using the returned read bytes count. A file with > 1024 bytes will reuse the now non-0 buffer and print unintended values. Fix buffered reading to be technically correct and work for arbitrary file contents. Fixes #4141
1 parent 22f5509 commit 01cefbe

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

snippets/csharp/System.IO/FileStream/Overview/fstream class.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public static void Main()
3535
{
3636
byte[] b = new byte[1024];
3737
UTF8Encoding temp = new UTF8Encoding(true);
38-
while (fs.Read(b,0,b.Length) > 0)
38+
int readLen;
39+
while ((readLen = fs.Read(b,0,b.Length)) > 0)
3940
{
40-
Console.WriteLine(temp.GetString(b));
41+
Console.WriteLine(temp.GetString(b,0,readLen));
4142
}
4243
}
4344
}

0 commit comments

Comments
 (0)