News:

Welcome to RetroCoders Community

Main Menu

Print Whole Text File to Terminal

Started by stigma, Jan 22, 2023, 04:00 PM

Previous topic - Next topic

stigma

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;
}