0

I'm reading a program in C and I want to locate the new line, but i don't know how.

I'm reading the text file like this:

    while(!feof(fp)){
         fgets(buffer, 4, fp);
    }

and my text file is like this:

C01 RDA ALB NAC EDF
C02 MCA EDF SLF ADG

how would I know if the word being read by fgets is in a new line??

2 Answers2

0

You're not supposed to call feof() before doing I/O, that's simply not how it works.

If you're that interested in the location of each newline, read the file character by character instead, using fgetc(). Then parse the character stream into words. This will make it trivial to notice when a newline comes along.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Do you really want to get three characters at a time (which is what you get for size 4, because fgets reads one less than size characters), thus getting the pieces

C01  RD A A LB  NAC  ED C02  MC A E DF  SLF  AD

( denoting newline)? I don't think so, but rather you want four characters at a time and trim the word delimiters.

char buffer[5];
char newline = 1;
while (fgets(buffer, sizeof buffer, fp))
{
    if (newline) puts("new line:");
    newline = buffer[3] == '\n';
    buffer[3] = '\0';
    puts(buffer);
}
Armali
  • 18,255
  • 14
  • 57
  • 171