RetroCoders Community

C / C ++ Programming => C / C++ => Topic started by: stigma on Jan 22, 2023, 04:00 PM

Title: Print Whole Text File to Terminal
Post by: stigma on Jan 22, 2023, 04:00 PM
This is a useful function that will print the content of a text file to the console (cmd or terminal)

just remember to add #include <string.h> at the top

char* read(const char* filename){
    FILE* f = fopen(filename, "rb");
    if (f == NULL){
        exit(1);
    }
    fseek(f, 0L, SEEK_END);
    long size = ftell(f)+1;
    fclose(f);
    f = fopen(filename, "r");
    void* content = memset(malloc(size), '\0', size);
    fread(content, 1, size-1, f);
    fclose(f);
    return (char*) content;
}