Re: fgets question



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
.



Relevant Pages

  • Re: Is this string input function safe?
    ... return a pointer to mallocated memory holding one input string, ... See my comment after your call to fgets. ... char* malloc_getstr ... before any characters are read, then the ...
    (comp.lang.c)
  • Re: Is C99 the final C? (some suggestions)
    ... fgets was designed to read text lines into a string. ... that fgetsdoes not ignore embedded null characters in text files. ... and puts it in the buffer nominated. ...
    (comp.lang.c)
  • Re: Help a beginner - simple lowercase to uppercase and so on function
    ... And then one to loop across the string calling that function ... copying at to a new array. ... are any characters other than lowercase letters */ ...
    (comp.lang.c)
  • Re: Help creating a random string in Perl
    ... can pick one of the 7 characters in my source string) by using "print ... third and fourth characters to and check that the random number hasn't ... shuffling the array ... The idea here is to swap the current cell with some cell ...
    (perl.beginners)
  • Re: newbie: mapping CHARACTER*2 to INTEGER*2
    ... that destroys the Chinese characters in my input. ... effect the conversion required. ... If MOLD is an array and SIZE is omitted, ... represent the values 4.0 and 1082130432 as the string of binary digits ...
    (comp.lang.fortran)