Created
March 9, 2019 19:30
-
-
Save a2gs/7f5bdb906ef5d227e174d6021f3ad7c1 to your computer and use it in GitHub Desktop.
Loop above fgets() return instead feof()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
gcc -o loopFgets loopFgets.c -g -std=c11 -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-parameter -Wno-unused-result -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -D_POSIX_SOURCE=1 -D_DEFAULT_SOURCE=1 -D_GNU_SOURCE=1 | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <errno.h> | |
#define MAX_BUFF (10000) | |
int main(int argc, char *argv[]) | |
{ | |
FILE *f = NULL; | |
char *c = NULL; | |
unsigned int i = 0; | |
char buff[MAX_BUFF + 1] = {'\0'}; | |
if(argc != 2){ | |
printf("Usage:\n%s <FILE>\n", argv[0]); | |
return(-1); | |
} | |
f = fopen(argv[1], "r"); | |
if(f == NULL){ | |
printf("Unable to open file: [%s]\n", strerror(errno)); | |
return(-1); | |
} | |
for(i = 0; fgets(buff, MAX_BUFF, f) != NULL; i++){ | |
c = strchr(buff, '\n'); | |
if(c != NULL) *c = '\0'; | |
printf("%04d: [%s]\n", i, buff); | |
} | |
fclose(f); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment