You need to enlarge the character array where you are reading data.
The first call of fgets
reads exactly 29 (one more character is reserved for the terminating zero character '\0'
) characters that present in the first record of the file before the new line character '\n'. The second call reads this new line character.
From the C Standard (7.21.7.2 The fgets function)
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.
Declare the array for example like
char buff[100];
and call fgets
like
fgets( buff, sizeof( buff ), fptr );
Here is a demonstrative program that shows that the array with 30 elements is not large enough to read the whole first record. That is that the first record contains more than 30 characters.
#include <stdio.h>
#include <string.h>
int main(void)
{
const char record[] = "hello world! Learning to code!\n";
printf( "strlen( record ) = %zu\n", strlen( record ) );
return 0;
}
The program output is
strlen( record ) = 31