how can i reset fgetl to the beginning of the text file. frewind doesn't work
7 views (last 30 days)
Show older comments
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 Comments
Les Beckham
on 1 Apr 2022
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
on 1 Apr 2022
"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
Answers (1)
Ayush Modi
on 1 Jan 2024
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 Comments
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!