Expected file position after FSCANF conversion failure

1 view (last 30 days)
Suppose the file data.txt contains the lines
--VarX
0.0 0.0 1.5 10.3
Is there an expected output of the following script
fid = fopen('data.txt');
[v, n] = fscanf(fid, '%f');
ftell(fid)
fclose(fid);
? Obviously, the FSCANF call will fail to convert any floating point numbers upon encountering the '--' string whence isempty(v) and n==0. However, I'm currently most concerned about the FTELL call. In MATLABs 7.7 (R2008b) and 7.9 (R2009b), FTELL returns 1 while MATLABs 7.10 (R2010a) and 7.13 (R2011b) both return 0.
In other words, R2009b (and earlier editions) advances the file pointer one byte, presumably because '-' is a valid leading character sequence of a (negative) floating point number. Later M-editions do apparently not advance the file pointer however, presumably because '--' is not a valid leading character sequence of a floating point number.
So, to repeat my above question: is there any defined behaviour with respect to FTELL in case of FSCANF conversion failure and, if so, did this behaviour change between R2009b and later editions of MATLAB? I cannot find anything in doc fscanf, doc ftell or the release notes.
As far as I can tell, MathWorks Technical Solution ID 1-426ARH is related to this question, but does not appear to be exactly the same thing. Am I just misreading the information therein?
PS Before you ask: For various reasons (unrelated to this particular issue) I cannot simply use
textscan(fid, '%f', 'CommentStyle', '--')
in the general case I'm trying to solve.

Accepted Answer

Bård Skaflestad
Bård Skaflestad on 30 Jan 2012
In case anyone else encounters the issue, I asked this question to the MathWorks support team. MATLAB R2010a corrected a bug whereby fscanf would consume characters from the input stream even in the case of a conversion failure.
Officially, ftell is supposed to return 0 in the example I provided originally. Do be aware, however, that releases prior to R2010a may return 1. As such, I think it is better to not write code that depends on a particular return value from ftell in case of an fscanf input failure.

More Answers (0)

Categories

Find more on Data Type Conversion 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!