You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
nfbot
changed the title
When reading text file with StreamReader, FileStream position resets to 0 periodically creating endless loop in the reader.
When reading text file with StreamReader, FileStream position resets to 0 periodically creating endless loop in the reader
Aug 7, 2024
I noticed the issue with StreamReader and FileStream and believe it would be a great first issue for me to work on. I'm eager to contribute and would love the opportunity to tackle this.
Hey @Akshit018!
That's great that you're willing to tackle this one. 🥳
I'm assigning it to you now.
Please take a look at the contribution guide here. This will typically require a PR to the Sys.IO.FileSystem repo.
We 💜 PRs so feel free to start one as early as you want so you can have early feedback.
If you have questions or want to discuss anything, suggest you head over to our Discord server and engage a conversation there.
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
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
Flash to ESP32 with:
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
The text was updated successfully, but these errors were encountered: