Home
Home copied to clipboard
When reading text file with StreamReader, FileStream position resets to 0 periodically creating endless loop in the reader
Library/API/IoT binding
System.IO.FileSystem, System.IO.Streams
Visual Studio version
VS2022
.NET nanoFramework extension version
No response
Target name(s)
ESP32_REV3
Firmware version
1.12.0.32
Device capabilities
No response
Description
Reading text file with StreamReader, the underlying FileStream resets its position to 0 every time StreamReader's buffer is filled up. This causes endless jumps to beginning of the file and thus the file can't be read to end.
How to reproduce
- Upload text file to ESP32 config partition. The file must be longer than StreamReader's buffer size (512B).
- Open the file and read it line-by-line with StreamReader.ReadLine().
Expected behaviour
File is correctly read line-by-line from start to end. When end of file is reached ReadLine() returns null.
Screenshots
Sample project or code
//The last line of loremipsum.txt is <END>
[TestMethod]
public void ReadText_StreamReader()
{
Debug.WriteLine(nameof(ReadText_StreamReader));
string line;
string text = "";
using (var fs = new StreamReader(new FileStream(@"I:\loremipsum.txt", FileMode.Open, FileAccess.Read)))
{
line = fs.ReadLine();
int i = 0;
while (line != null && i++ < 50) //exit after reading 50 lines to avoid endless loop
{
Debug.WriteLine(line);
line = fs.ReadLine();
if (line != null)
text = line;
}
}
Assert.Equal("<END>", text);
}
[loremipsum.txt](https://github.com/user-attachments/files/16523448/loremipsum.txt)
Flash to ESP32 with:
@echo off
set port=COM5
littlefs-python create ..\FileSystem %tmp%\config_image.bin --fs-size=262144 --block-size=4096
python %idf_path%\components\partition_table\parttool.py -p %port% write_partition -n config --input %tmp%\config_image.bin
del %tmp%\config_image.bin
Aditional information
Workaround: Track stream's position in a separate variable and set the stream position before each read: https://github.com/CShark-Hub/Mako-IoT.Device.Services.FileStorage/blob/main/src/MakoIoT.Device.Services.FileStorage/MakoStreamReader.cs#L525
Note: This was working OK with one of the previous versions of System.IO.FileSystem