Instantly share code, notes, and snippets.

@jjwest /main.c
Created Dec 27, 2018

Embed
What would you like to do?
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