Home > Enterprise >  not able to read a file by repetitive use of fgets
not able to read a file by repetitive use of fgets

Time:09-27

In this code, I am trying to read data from the file (30 characters in each line) with content:

hello world! Learning to code!

123456789123456789123456789123

The code used to read this file is:

#include<stdio.h>
#include<stddef.h>

int main(void)
{
    FILE *fptr = fopen("n","r");
    char buff[30];
    char *buff_ptr = buff;
    size_t buff_size=30;
    size_t character_count=0;

    buff_ptr = fgets(buff,buff_size,fptr);
    printf("%s\n",buff_ptr);

    fgets(buff,buff_size,fptr);
    printf("%s\n",buff);
    return 0;
}

Why is there is no output being printed for the second fgets?

CodePudding user response:

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
  • Related