help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how does fscanf with "C" work?


From: Francesco Potortì
Subject: Re: how does fscanf with "C" work?
Date: Tue, 09 Sep 2014 11:21:08 +0200

>> I'll try to be more specific.  If I get no answer from this list, I'll
>> submit a bug report.
>>
>> I create a text file made of four lines, like this:
>>
>> ===File ~/math/workarea/test.txt============================
>> field1, field2, field3, field4
>> A,        a,        1,       1.0,
>> B,        b,        2,        2.0,
>> C,        c,        3,        3.0,
>> ============================================================
>>
>> Then this is what i get:
>>
>> octave> version
>> ans = 3.8.2
>> octave> fid=fopen("test.txt");
>> octave> fgetl(fid);
>> octave> [v1, v2, v3, v4, count, errmsg] = fscanf(fid, "%s%s%d,%f,", "C")
>> v1 = A,
>> v2 = a,
>> v3 =  1
>> v4 =  1
>> count =  4
>> errmsg =
>> octave> fclose(fid);
>
>I have not been following this, but, fscanf is doing exactly what you 
>asked it to.

Yes, apparently it is so.  I'll propose a clarification to the manual
once I understand exactly what's going on.

>Read 'help fscanf' carefully.  You are using the second form.
>
>fd=fopen("xx.dat")
>fgetl(fd)
>[v1, v2, v3, v4, count, errmsg] = fscanf(fd, "%s%s%d,%f", "C")
>
>This is iterative and so, slow.

Yes.  However, I was assuming that scanf in the "C" form behaved like
the first form, that is, kept reading its input until end of file.  This
would have been specular to the printf function, which reuses its
template when given a matrix to print.  Moreover, the manual names V1,
V2 the returned values, suggesting that they are vectors.  From my
experiments and your answer, it appears that my understanding was wrong.

>You can read the entire file using the first form of fscanf.  However, 
>since the strings are not quoted, octave will translate the ascii 
>characters to numerical byte values.
>A==65, B==66, etc.

This is new to me, and I cannot find it in the manual.  Again, once I
completely understand this, I'll propose a correction to the manual.

>fd=fopen("xx.dat")
>fgetl(fd)
>[A,count,msg]=fscanf(fd,"%s%s%d,%f%s")
>
>The contents of A show this conversion and also, the extra ',' on each 
>line that must be accounted for in the format.

Alright, I'll make experiments and get back here.

>By the way, your first paragraph sounds threatening and makes many 
>people not answer. 

Oops, sorry, that was certainly not my intention :)

I meant that if people do not answer some question here, probably no one
knows, and maybe there is a bug...

-- 
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it




reply via email to

[Prev in Thread] Current Thread [Next in Thread]