Skip to content

Instantly share code, notes, and snippets.

@amattu2
Created December 20, 2020 14:42
Show Gist options
  • Save amattu2/91fbbdb8aeb8ae304f9583f3250bf714 to your computer and use it in GitHub Desktop.
Save amattu2/91fbbdb8aeb8ae304f9583f3250bf714 to your computer and use it in GitHub Desktop.
Read the specified file into the console (stdout).
/*
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