Discussion:
Unexpected Output Behavior
(too old to reply)
c***@gmail.com
2009-01-28 20:47:12 UTC
Permalink
I need a octave script that can read a value from stdin,
print it, and continue this cycle until eof is reached.


My code:

while (!feof(stdin))

z = fscanf(stdin, "%f\n");

printf(">> %f <<\n", z);
printf("hello\n");

endwhile

The output for input values of "1", "2", and "3", ^D
1.000000 <<
2.000000 <<
3.000000 <<
hello


There are two problems with this.

First, nothing is printed until ^D is reached.
And second, why is "hello" printed only once?

I've tried configuring the pager settings,
but I don't believe that is the problem.

I've also tried fflush(stdout) after the fprint() statements with no
effect.


Any help would be much appreciated.
Francesco Potortì
2009-01-29 09:02:44 UTC
Permalink
Post by c***@gmail.com
while (!feof(stdin))
z = fscanf(stdin, "%f\n");
printf(">> %f <<\n", z);
printf("hello\n");
endwhile
The output for input values of "1", "2", and "3", ^D
1.000000 <<
2.000000 <<
3.000000 <<
hello
To me, this looks consistent with fscanf's documentation. In fact, you
run through the loop once only, because fscanf by default reads until it
finds things to read:

-- Built-in Function: [VAL, COUNT] = fscanf (FID, TEMPLATE, SIZE)
-- Built-in Function: [V1, V2, ..., COUNT] = fscanf (FID, TEMPLATE,
"C")
In the first form, read from FID according to TEMPLATE, returning
the result in the matrix VAL.

The optional argument SIZE specifies the amount of data to read
and may be one of

`Inf'
Read as much as possible, returning a column vector.

`NR'
Read up to NR elements, returning a column vector.

`[NR, Inf]'
Read as much as possible, returning a matrix with NR rows.
If the number of elements read is not an exact multiple of
NR, the last column is padded with zeros.

`[NR, NC]'
Read up to `NR * NC' elements, returning a matrix with NR
rows. If the number of elements read is not an exact multiple
of NR, the last column is padded with zeros.

If SIZE is omitted, a value of `Inf' is assumed.

A string is returned if TEMPLATE specifies only character
conversions.

The number of items successfully read is returned in COUNT.

In the second form, read from FID according to TEMPLATE, with each
conversion specifier in TEMPLATE corresponding to a single scalar
return value. This form is more `C-like', and also compatible
with previous versions of Octave. The number of successful
conversions is returned in COUNT

*See also:* scanf, sscanf, fread, fprintf, fgets, fputs.
c***@gmail.com
2009-01-29 14:44:41 UTC
Permalink
 -- Built-in Function: [V1, V2, ..., COUNT] = fscanf (FID, TEMPLATE,
     In the second form, read from FID according to TEMPLATE, with each
     conversion specifier in TEMPLATE corresponding to a single scalar
     return value.  This form is more `C-like', and also compatible
     with previous versions of Octave.  The number of successful
     conversions is returned in COUNT
Thank you very much. This solved my problem.

Loading...