fgetl
John W. Eaton
jwe at octave.org
Mon Feb 9 12:41:06 CST 2009
On 9-Feb-2009, Olaf Till wrote:
| In Octave-3.02, "fgetl (stdin)" behaves not as expected:
|
| octave:1> fgetl (stdin)
| a
|
| ans = a
| octave:2> fgetl (stdin)
| a
| ans =
| octave:3> fgetl (stdin)
| a
| ans = a
| octave:4> fgetl (stdin)
| b
| ans = a
|
| (In the first line I had to press Enter twice.)
The function that does the reading is octave_base_stream::do_gets in
src/oct-stream.cc. After reading the first line (the characters 'a'
and '\n') the following code is executed:
if (! is.eof () && char_count > 0)
{
// GAGME. Matlab seems to check for EOF even if the last
// character in a file is a newline character. This is NOT
// what the corresponding C-library functions do.
int disgusting_compatibility_hack = is.get ();
if (! is.eof ())
is.putback (disgusting_compatibility_hack);
}
so Octave attempts to read another character for Matlab compatibility.
Note that your second read is returning the empty string, because the
second newline character you entered for the first call is still on
the input stream.
I see no good and simple fix for this problem, as I'm sure the reason
we have this extra code is due to a bug report, so changing it back
and breaking compatibility would probably just invite additional bug
reports about this feature.
FWIW, I think it is probably not a good idea to use fgetl to read
input from stdin when Octave is running interactively. Perhaps we
should make this an error instead of trying to allow it?
jwe
More information about the Bug-octave
mailing list