Re: fgets question
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Sun, 08 Jun 2008 21:26:04 -0500
On Mon, 09 Jun 2008 01:54:12 GMT, "Bill Cunningham" <nospam@xxxxxxxxx>
wrote in comp.lang.c:
I was talking with someone about fgets and he said that fgets puts the
\n in a string but not \0. I decided to test this assumption because my
documentation didn't say if fgets put \0 after a string literal.
Who was this person, and what credentials did he have to make you
think he was right?
More importantly, what does a string literal have to do with anything?
fgets() has nothing at all to do with string literals. It reads input
from a stream. String literals are quoted strings in your source
code, not input read from a stream.
Strlen was
what I used to decide the \0 was not added. How can it be added for a
string? My code.
I surely don't understand what you were trying to do above. If you
had an array of characters that does not contain at least one '\0'
within the bounds of the array, then it is not a string. Calling
strlen() on an array of characters that is not a string, due to the
lack of a '\0', produces undefined behavior. strlen() is for strings,
not arbitrary arrays of garbage characters.
int main (void) {
char input[10];
fgets (input, sizeof(input), stdin);
In this call, fgets() will accept up to 9 characters from stdin. Fewer
if it receives a '\n' before the ninth character.
It will store these characters into consecutive elements of the array.
Regardless of whether it stores 9 or fewer characters in the array, it
will place a '\0' into the array after the last character it stores.
Here is specifically what the standard says about fgets():
"The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream into
the array pointed to by s. No additional characters are read after a
new-line character (which is retained) or after end-of-file. A
null character is written immediately after the last character read
into the array."
So your "someone" is wrong in their opinion.
printf ("%i\n", strlen(input));
The strlen() function returns a value of type size_t, and since
printf() is a variadic function, the function call does not perform
any conversion. "%i" is a conversion specifier for signed int, and
size_t can never be a signed int since it is always an unsigned type.
If you need to print a reasonably small value of size_t, you could do
this:
printf("%u", (unsigned)strlen(input));
}
One more character than what was typed printf reported.
I can't parse the sentence you have written above.
It would be much better to:
1. Show us why you typed into the program
2. Show us what the output of the program was.
Regardless of what you think you proved to yourself with your program,
the fgets() function most surely does produce a '\0' terminated proper
string, except under two exceptional cases:
"The fgets function returns s if successful. If end-of-file is
encountered and no characters have been read into the array, the
contents of the array remain unchanged and a null pointer is returned.
If a read error occurs during the operation, the array contents are
indeterminate and a null pointer is returned."
Either of these exceptional cases is easily recognized by the fact
that fgets() returns NULL, instead of returning the pointer that was
passed in to it.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- Follow-Ups:
- Re: fgets question
- From: Bill Cunningham
- Re: fgets question
- References:
- fgets question
- From: Bill Cunningham
- fgets question
- Prev by Date: Re: fgets question
- Next by Date: Re: Lookup PID from Port.
- Previous by thread: Re: fgets question
- Next by thread: Re: fgets question
- Index(es):
Relevant Pages
|