I am learning C and I'm trying to understand the library.
For example, in The GNU C Library, it gives:
— Function: char * fgets (char *s, int
count, FILE *stream)
The fgets function reads characters
from the stream stream up
to and including a newline character
and stores them in the string s,
adding a null character to mark the
end of the string. You must supply
count characters worth of space in s,
but the number of characters read is
at most count − 1. The extra character
space is used to hold the null
character at the end of the string.
If the system is already at end of file
when you call fgets, then the
contents of the array s are unchanged
and a null pointer is returned. A null
pointer is also returned if a read
error occurs. Otherwise, the return
value is the pointer s.
Warning: If the input data has a null
character, you can't tell. So
don't use fgets unless you know the
data cannot contain a null. Don't use
it to read files edited by the user
because, if the user inserts a null
character, you should either handle it
properly or print a clear error
message. We recommend using getline
instead of fgets.
However, I've still seen people use fgets to collect input from console other than a file, eg.:
fgets(line,sizeof(line),stdin);
sscanf(line,"%d",¤t); // read the input
Can someone explain to me how should I read and follow the grammar of the C language? Should you learn C through imitating others' code or by actually referencing the Library and then writing code yourself?
Cheers,
No comments:
Post a Comment