C - fgets() - What if the string is longer?

5.6k Views Asked by At

Let's say i have a file containing a string of length 10 :

abcdefghij

And read my text file line by line using fgets() with a buffer of size 4. I will have to call fgets() in a loop to make sure the whole line is read. But, at say the first call, it will read the first 3 characters (buffer size -1) right ? Will it also append a null terminating character at the last position of my 4 char buffer even if the real end of my string wasn't reached, or will fill it with 4 characters and no null terminating characters at the end of my buffer ? which would make a call to strlen() impossible ?

Thank you :)

4

There are 4 best solutions below

0
On

Looking at the man page of fgets you can see :

"The newline, if any, is retained. If any characters are read and there is no error, a `\0' character is appended to end the string"

(http://www.manpagez.com/man/3/fgets/)

0
On

From C standard draft (2010) n1547.pdf, 7.21.7.2.2:

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.

1
On

It is in the documentation.

Verbatim from man fgets (italics by me):

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.


Verbatim from the HP-UX 11 man page (italics by me):

fgets()

Reads characters from the stream into the array pointed to by s, until n-1 characters are read, a new-line character is read and transferred to s, or an end-of- file condition is encountered. The string is then terminated with a null character.


From the POSIX specs:

The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte.


Last not least from MSDN:

The fgets function reads a string from the input stream argument and stores it in str. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in str is appended with a null character. The newline character, if read, is included in the string.

0
On

I used fseek (moves a pointer in the file). Program looked like (pseudo-code):

read string[default length value];
while string length == default length value
{
    resize string;
    move back to the line start with fseek;
    read string;
}

To I used malloc and realloc to resize the string (which is a pointer).