Re: *scanf in Harbison and Steele
- From: George <george@xxxxxxxxxxxxxxx>
- Date: Sat, 29 Nov 2008 13:50:43 -0700
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/
.
- References:
- *scanf in Harbison and Steele
- From: George
- Re: *scanf in Harbison and Steele
- From: pete
- Re: *scanf in Harbison and Steele
- From: George
- Re: *scanf in Harbison and Steele
- From: James Kuyper
- *scanf in Harbison and Steele
- Prev by Date: Re: What is this?
- Next by Date: z error
- Previous by thread: Re: *scanf in Harbison and Steele
- Next by thread: Re: *scanf in Harbison and Steele
- Index(es):
Relevant Pages
|