Skip to content

Instantly share code, notes, and snippets.

@jjwest
Created December 27, 2018 20:31
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 jjwest/c59d7d8889f25fcac22a09b7c5a7b4ec to your computer and use it in GitHub Desktop.
Save jjwest/c59d7d8889f25fcac22a09b7c5a7b4ec to your computer and use it in GitHub Desktop.
C file io
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *filename = "Hello.txt";
printf("In file %s\n", filename);
FILE *file = fopen(filename, "r");
fseek(file, 0, SEEK_END);
size_t len = ftell(file);
fseek(file, 0, SEEK_SET);
char *content = malloc(len + 1);
fread(content, len, 1, file);
content[len] = 0;
printf("File read completed\n%s", content);
free(content);
fclose(file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment