1

First of all I'd like to tell that I haven't "looked inside" of the sgets() function. Here is code:

#include <iostream>
#include <stdio.h>

#pragma warning(disable:4996)

int main(){
    FILE* file;
    int count = 1;
    char buf[256];
    if (file = fopen("file.txt", "r"))
        while (!feof(file))
        {
            fgets(buf, 256, file);
            printf("%d string: %s", count, buf);
            ++count;
        }
    fclose(file);
    return 0;
}

this is what I wrote in the file.txt:

I
was
born
here
.

And the output in cmd is:

1 string: I
2 string: was
3 string: born
4 string: here
5 string: .
6 string: .

How can I refuse doubling of 5th string?

Vadym
  • 548
  • 2
  • 8
  • 21

1 Answers1

1

You can make the below changes to see your code work as expected

    while (fgets(buf, 256, file) != NULL)
    {        
        printf("%d string: %s", count, buf);
        ++count;
    }

As already mentioned don't use feof() as done in your code.

Gopi
  • 19,784
  • 4
  • 24
  • 36