0

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?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Agrudge Amicus
  • 1,033
  • 7
  • 19
  • Do your file really have a blank line in it? Perhaps you print that blank line and think its not printing anything? And what does `fgets` *return*? It can fail and will then return `NULL`, something which you always needs to check for (as well as check that `fopen` doesn't fail as well). – Some programmer dude Sep 26 '21 at 09:07
  • @Someprogrammerdude `fgets` is returning the pointer to the buffer where the content is being saved and there is no blank line in the text file which is being read. – Agrudge Amicus Sep 26 '21 at 09:09
  • 2
    30 is not a happy magic number in this code, you need at least 32 to read one line. One extra for the line ending, one extra for the zero terminator. – Hans Passant Sep 26 '21 at 09:09
  • @user3121023 so if another `fgets` is added to the code then the next line from the file will be read? Also, why this issue doesn't occur when `getline` is used? – Agrudge Amicus Sep 26 '21 at 09:11
  • Know, you can read the entire file, 29 bytes at a time.. Nothing wrong with that, you just end up calling `fgets()` more often resulting in a slight loss in efficiency. Lesson: **Don't Skimp on Buffer Size**. `1024` is a nice 1K buffer or `2048` for a 2K buffer. – David C. Rankin Sep 26 '21 at 09:19
  • Your question details do not match the results. The 2nd fgets would read the ! which is the 30th character on the first line. – Antti Haapala -- Слава Україні Sep 26 '21 at 14:17

1 Answers1

2

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335