fgets()
root@void:~ > cat a hogehoge fugafuga piyopiyo root@void:~ > cat x.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(void) { int i; char buf[1024]; FILE *fp; fp = fopen("a","r"); if (fp == NULL) { fprintf(stderr, "failed to open file.\n"); return -1; } while (fgets(buf, 1024, fp) != NULL) { printf(buf); } printf("!!!EOF!!!\n"); printf("Retry to read data 5 times...\n"); for (i = 0; i < 5; i++) { fgets(buf, 1024, fp); printf(buf); } printf("Retry to read data 5 times with mod...\n"); buf[1] = '\n'; buf[2] = '\0'; for (i = 0; i < 5; i++) { fgets(buf, 1024, fp); printf(buf); } fclose(fp); return 0; } root@void:~ > cc -o x x.c root@void:~ > ./x hogehoge fugafuga piyopiyo !!!EOF!!! Retry to read data 5 times... piyopiyo piyopiyo piyopiyo piyopiyo piyopiyo Retry to read data 5 times with mod... p p p p p root@void:~ >
ヽ(´ー`)ノ