Skip to content

Instantly share code, notes, and snippets.

@shawnanastasio
Last active April 25, 2016 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnanastasio/914ae1dfc605d1faa39db0dd9a16bfbe to your computer and use it in GitHub Desktop.
Save shawnanastasio/914ae1dfc605d1faa39db0dd9a16bfbe to your computer and use it in GitHub Desktop.
Read a file line-by-line in C
/**
* Reads a file line-by-line.
* smh
* Created by Shawn Anastasio.
* Licensed to Public Domain.
*/
#include <stdio.h>
#define MAX_FILE_SIZE 1024
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
int cur_line = 0;
char buffer[MAX_FILE_SIZE];
FILE *fp;
fp = fopen(argv[1], "r");
while(fgets(buffer, MAX_FILE_SIZE, fp)) {
printf("Just read line #%d\n", ++cur_line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment