-1
#include <stdio.h>
#define BUFFER_SIZE 80
int main()
{
   FILE* fp = fopen("fp.txt" ,"r");
   char buf[BUFFER_SIZE];
   if(fp!=NULL)
    {
         while (!feof(fp))
            {
                fscanf(fp ,"%s",buf);
                  printf("Read line: %s\n" ,buf);\
                  printf("\n");
            }
    }
    fclose(fp);
}
//need create fp file 
    UW
    CSE     

how to stop CSE dont repeat CSE again. Can you use condition if-else do-while etc.

1 Answers1

0

Please see Why is “while ( !feof (file) )” always wrong? The function feof does not do what many beginners imagine.

The reason why you repeat the input CSE, is because after you read beyond the end of the file, that data remains in the buffer.

I advise you to change the loop to

while (fscanf(fp, "%79s", buf) == 1) {
    printf("Read line: %s\n", buf);
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56