Last active
April 25, 2016 01:03
-
-
Save shawnanastasio/914ae1dfc605d1faa39db0dd9a16bfbe to your computer and use it in GitHub Desktop.
Read a file line-by-line 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
/** | |
* 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