Created
August 18, 2020 14:58
-
-
Save zchrissirhcz/cd4c8c16cca917803adc41342bfab200 to your computer and use it in GitHub Desktop.
Read text file line by line, implemented in C.
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
/* | |
Read text file line by line, implemented in C. | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdbool.h> | |
int main() { | |
const char* txt_pth = "input.txt"; | |
FILE* fin = fopen(txt_pth, "r"); | |
char line[1024]; | |
int buf_size = sizeof(line); | |
void* ret = NULL; | |
while(true) { | |
memset(line, 0, buf_size); | |
ret = fgets(line, buf_size, fin); | |
if(ret==NULL) break; | |
line[strlen(line)] = '\0'; // trim '\n' | |
printf("you read %s\n", line); | |
} | |
fclose(fin); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment