Re: *scanf in Harbison and Steele



On Sat, 29 Nov 2008 03:26:07 GMT, James Kuyper wrote:

George wrote:
...
I had a different question though. I'll establish relevance to the subject
later. The difference between fgets and gets is at least that fgets takes
a file pointer as an arg. gets uses stdin.

That's one key difference. Another difference is that fgets() takes a
count argument, and will not write more than the specified number of
bytes to the buffer, which is the key feature that makes fgets() safer
than gets(). The final differences is that gets() removes the newline
character that caused it to stop reading. fgets() writes that newline to
the output buffer. Depending upon what you're going to do with the
buffer, that might not matter at all. In most such cases in my code, the
newline character doesn't matter at all.

If you wanted to use fgets and stdin, how do you do it?

Though I've been told that stdin come with the food in C, must you declare:

extern *file stdin;

No. First of all, that would be the wrong declaration (it should be
FILE*, not *file). Secondly, you should not define it; that's the job of
<stdio.h>.

char *s buffer;

The 's' is a typo, I hope. If not, you'll have to explain what it is.

The s was for char star buffer. It's too intentional for me to claim as a
typo.:-( Pointer declarations are not my strong suit.


int n;
file * f;

f= stdin;

What purpose does 'f' serve? Why not use stdin directly down below?

It is meant to mimic Chuck's fggets. I don't want to bring up the
varieties of *gets() again. Chuck, Richard, Pete, Eric Sosman and Morris
Dovey have variations on it posted.


...
(give values to other declared vars)

I presume that this includes setting buffer to point at an actual block
of memory?

LHS = fgets( buffer, n, f);

There's no need for storing the LHS. It should be either NULL or compare
equal to buffer; if it doesn't, the value is meaningless. So the only
useful thing to do with the return value is to compare it with either
NULL or buffer. You can do that immediately, without bothering to store it.

For reasons I've explained elsewhere today in a different context, I
prefer to compare versus the successful return value, treating all other
values as exceptional cases of some kind.

if(fgets(buffer, n, stdin) == buffer)
{
// Normal processing
}
else if(ferr(stdin))
{
// I/O error handling
}
else
{
// End of file handling.
}

Thanks, james, I think I've got this now. The thrust of the question is
whether you had to declare stdin or not, to which your answer is, I believe
"don't declare stdin; #include <stdio.h> instead.

I appreciate the lack of ridicule for my other slipshod declarations.
--
George

I have a different vision of leadership. A leadership is someone who brings
people together.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
.



Relevant Pages

  • Re: *scanf in Harbison and Steele
    ... The difference between fgets and gets is at least that fgets takes ... Another difference is that fgetstakes a count argument, and will not write more than the specified number of bytes to the buffer, which is the key feature that makes fgetssafer than gets. ... Though I've been told that stdin come with the food in C, ... So the only useful thing to do with the return value is to compare it with either NULL or buffer. ...
    (comp.lang.c)
  • Detecting a PIPE in a program
    ... while (fgets (buffer, sizeof(buffer), stdin)!= NULL) ... Ant sample "C" code tidbets would be appreciated. ...
    (comp.lang.c)
  • Re: Best way to input from stdin?
    ... that tests the return of fgets(), ... // read the buffer ... A quick google on the subject failed to return any meaningful result and searching this group's history through google groups ended up being a very disappointing experience (a search for nothing more than "stdin" returns only 14 results that only go as far as June 8th). ...
    (comp.lang.c)
  • Re: Question on some C code that I saw
    ... int main(int argc, char *argv) ... while(fgets(buffer, sizeof buffer, stdin)) ... which copies stdin both to stdout and to a named ... The use of fgets is probably just meant to demonstrate the use ...
    (comp.lang.c)
  • system() and _flushall()
    ... You must explicitly flush (using fflush or _flushall) or close any stream ... However, on the next read from the input file using fgets, I get ... forgotten that there's still data in the buffer (the buffer is not destroyed ...
    (microsoft.public.vc.language)