how can i reset fgetl to the beginning of the text file. frewind doesn't work
9 次查看(过去 30 天)
显示 更早的评论
fgetl ist always -1 and the number of my open document is counted higher with every loop and I don't know how to reset?
2 个评论
Les Beckham
2022-4-1
You are more likely to get a good answer if you show the code and any errors or warnings that you get when you run it (all of the red or orange text in the command window). You can add the code in a comment or edit your question.
Also, please clarify what you mean by "the number of my open document is counted higher with every loop".
Voss
2022-4-1
"my open document is counted higher with every loop" sounds to me like an fclose() is missing, or perhaps it is there but it is not reached because of an error that occurs before it but after its corresponding fopen()
You can close all open files using fclose all
回答(1 个)
Ayush Modi
2024-1-1
Hi Jonas,
As per my understanding, you would like to reset the file position indicator to the beginning of the file and read the file using “fgetl” function. You can achieve this using the “fseek” function. Here is an example to demonstrate how you can accomplish this:
fid = fopen('yourfile.txt', 'rt');
status = fseek(fid, 0, 'bof');
Please refer to the following MathWorks documentation for more information on “fseek” function:
Note: The third argument in “fseek” function, “bof”, stands for the beginning of the file.
Also, if “fgetl” returns -1 right from the start, it could indicate that “fopen” is attempting to read an empty file.
Regarding the "open document is counted higher with every loop" issue, Voss has already provided an answer for that part.
I hope this resolves the issue you were facing.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!