Hello, again!

I have a problem in reading a line with fgets. The program is asking for five numbers to be put in. So, if I put in 5 numbers like ;
2222  333  4532    -4456        67554
The outcome will be :
2222+333+4532-4456+67554=70185

So, far so good. This works fine.
The routine that starts to read this line is as follows:

inimage:
                pushl    stdin
                pushl    $79
                pushl    $Buf   
                call        fgets
                addl      $12, %esp



But if I extend (the program has to be able to do this) the spaces inbetween the numbers I will go beyond the limit of the buffer,
2222          333                            4532                              -4456              67554

So, somewhere between -4456 and 67554 I have passed $79 and fgets will produce EOL and 0. How do I get by this problem. I e fgets has to continue reading till it has read everything?

I have tried different scenarios here with a loop for example but I can't get it to work. Anyone?

Anders
Posted on 2011-11-10 08:24:35 by anders11
fgets grabs an entire delimited string from the file... quits whenever it sees an end of line character (13, 10 or both I guess).
I have no idea how it reacts to the last line of the file not being terminated.
use fgetc ? use something else? fread? ReadFile? something else?
Posted on 2011-11-11 02:06:29 by Homer
As I understand it, the issue is not the missing EOL, but an attempt by the pesky user to overflow the buffer.

What I would have done, in the first place, is get one number per input, rather than trying to pick out up to five numbers - and an indefinite number of spaces between them - from a single input string. I guess the "specification" for the program (the homework assignment) requires it(?).

What are we "supposed" to do if the user enters a character which is neither a decimal digit nor a space (nor a linefeed)? What are we "supposed" to do if the user attempts to enter a number too long to fit in 31 bits (signed)? Might as well think about dealing with those at the same time...

What you'll have to do is get a bufferfull, process it, and fgets another... until done. Since you're picking multiple numbers out of the string, and need to be prepared to fgets more in the middle of a number, this could be quite a PITA!

As Homer suggests, you might want to think about using fgetc instead. Don't buffer input at all (no danger of overrunning the buffer! :) ). If you're getting decimal digits, add 'em into the "current number". When you get a space (or any non-digit?), store the current number and prepare to start the next one... once you start getting digits again. When you get the linefeed, user is done. Would that work?

Making the buffer big enough so the user gets bored typing spaces before filling the buffer would be considered sloppy programming! :)

Best,
Frank

Posted on 2011-11-11 03:04:41 by fbkotler
In regard of the buffer problem, consider using a self expanding buffer or autobuffer like this one.

Biterider
Posted on 2011-11-11 03:34:49 by Biterider