Trouble with fscanf

I am an occasional user. In the past I have used the following commands:

fix = fopen ("x.dat"');
x = fscanf (fix, "%f");

At this point the error “error fscanf:: invalid stream number -1” is returned.

In this case, the X.dat file is a column file of 22 numbers, format f6.7.

The OS is PopOS.
Gnu Octave 7.2.0 was downloaded through PopOS.

In January, I ran this in what may have been a previous version of Octave and it worked without a problem. I looked through the documentation and tried several perturbations of fscanf, to no avail.

I expect it is something simple I do not see and appreciate any help.

Thanks,

Herb

What is the value of fix ? It looks like the fopen command failed.

From the help fopen

help fopen
'fopen' is a built-in function from the file libinterp/corefcn/file-io.cc

 -- FID = fopen (NAME)
 -- [FID, MSG] = fopen (...)
     Open a file for low-level I/O or query open files and file
     descriptors.

     If an error occurs, FID is set to -1 and MSG
     contains the corresponding system error message.

Use the [fid, msg] = fopen(...) format of the command to find out why the fopen command failed.

1 Like

You use the file x.dat in the fopen command but say X.dat in your comment. Which is it supposed to be?

Yes, the problem was with opening the file. When I changed the LHS from “fix” to “[fix,msg]”, the fie was readable. No error was produced. Are the square brackets needed?

Thanks for the assistance. (I mis-typed the “X” in the comment.)

Herb

“fix” is a build-in octave function. It is a bad style to make variables that shadow build-in functions.

fix = fopen (“x.dat ” ' );
x = fscanf (fix, “ %f ” );

Is there a single quote in addition to the double quote ?

When I remove that single quote it works in my system.

>> [fix, msg] = fopen ("x.dat"')
fix = -1
msg = No such file or directory
>> [fix, msg] = fopen ("x.dat")
fix = 33
msg =
>>
1 Like