Created
December 27, 2018 20:31
-
-
Save jjwest/c59d7d8889f25fcac22a09b7c5a7b4ec to your computer and use it in GitHub Desktop.
C file io
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
#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