this program will keep reading next 20 lines until user enters 'q' or file reading is finished.
now for file reading is finished part, I've condition on line 29 of this program flag != NULL
as per my understanding fgets() returns NULL after it reach EOF but this condition doesn't work. what can I do to stop the loop after file has been read?
I'm using this file for testing and here's my program:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char name[66];
printf("Enter name of file: ");
scanf("%65s", name);
FILE *fp = fopen(name, "r");
if (!fp) {
printf("can't open file for reading\n");
return 1;
}
int i = 1;
char buffer1[1000];
char *flag;
char c;
do {
do {
i++;
flag = fgets(buffer1, 999, fp);
printf("%s", buffer1);
//printf("%d***\n", i); //debug - print line number
} while (i % 20 != 0 && flag != NULL);
scanf(" %c", &c);
} while (c != 'q');
return 0;
}
flags are typically boolean values, not pointers. Using moire appropriate names makes code much more clear and help spotting logical errors.