Created
December 20, 2020 14:42
-
-
Save amattu2/91fbbdb8aeb8ae304f9583f3250bf714 to your computer and use it in GitHub Desktop.
Read the specified file into the console (stdout).
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
/* | |
Produced 2020 | |
By https://amattu.com/links/github | |
Copy Alec M. | |
License GNU Affero General Public License v3.0 | |
*/ | |
/* | |
Description: | |
- Read the specified file into the console | |
- Max line length is set to 1001, although this can be easily | |
converted to a dynamic memory allocation | |
*/ | |
/* Files */ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
/* Main program function */ | |
int main(int argc, char *argv[]) { | |
/* Variables */ | |
FILE *file = NULL; | |
char line[1001]; | |
/* Check argument count */ | |
if (argc < 2) { | |
printf("No file argument provided to program\n"); | |
return 1; | |
} | |
/* Check file parameter */ | |
if (!(file = fopen(argv[1], "r"))) { | |
printf("No valid file provided to program\n"); | |
return 1; | |
} | |
/* Read file lines */ | |
while (fgets(line, sizeof(line), file)) { | |
printf("%s", line); | |
} | |
/* Default */ | |
fclose(file); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment