Why does the FSCANF function in MATLAB not leave the file pointer at the correct position when the format specifier includes trailing "skip" items?
1 view (last 30 days)
Show older comments
I have a text file that contains the following:
a;b;c;d;
I use the FSCANF function to read the "a" and "b", using a "skip" format to skip the semicolons:
out = fscanf(fid, '%c%*c', 2);
where "fid" is the file identifier of the file.
However, the file pointer, as returned by
ftell(fid)
is left at 3, just ahead of the second semicolon. I expected the file pointer to be left at 4, just after the second semicolon.
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
This is expected behavior: In the final repetition of the format string, FSCANF will stop reading after it has read the last unstarred (i.e. not skipped) format specifier or literal character in format string.
To force the file pointer to advance until the end of the format string, you can either:
1. Use literal characters to read the trailing non-captured items:
out = fscanf(fid, '%c;', 2);
2. Capture these items in the output, and programmatically manipulate the results to remove them
out = fscanf(fid, '%c%c', 4);
out = out(1:2:end);
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!