Skip to content

Instantly share code, notes, and snippets.

@nickrobson
Created March 27, 2015 01:20
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 nickrobson/f668ad947f902e732354 to your computer and use it in GitHub Desktop.
Save nickrobson/f668ad947f902e732354 to your computer and use it in GitHub Desktop.
Get the contents of a file.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
char fileName[30];
printf("Enter file name: ");
scanf("%s", fileName);
FILE* file = fopen(fileName, "r");
if (file == NULL) {
perror("Error while opening the file.\n");
return EXIT_FAILURE;
}
printf("Input characters: ");
char character;
while ((character = getc(file)) != EOF) {
// do something with the character! e.g. print it
printf("%c", character);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment