Why does the FSCANF function in MATLAB not leave the file pointer at the correct position when the format specifier includes trailing "skip" items?

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.

 Réponse acceptée

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);

Plus de réponses (0)

Produits

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by