Skip to content

Instantly share code, notes, and snippets.

@zchrissirhcz
Created August 18, 2020 14:58
Show Gist options
  • Save zchrissirhcz/cd4c8c16cca917803adc41342bfab200 to your computer and use it in GitHub Desktop.
Save zchrissirhcz/cd4c8c16cca917803adc41342bfab200 to your computer and use it in GitHub Desktop.
Read text file line by line, implemented in C.
/*
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