#include "helpers.h" #include #include char *readFileC(const char *filename){ //open file FILE *fp = fopen(filename, "r+"); if(!fp) return NULL; //go to end and get length fseek(fp, 0, SEEK_END); long len = ftell(fp); //allocate enough memory for the string on the heap char *ret = (char*)malloc(len); //go back to the beginning and read everything to the string fseek(fp, 0, SEEK_SET); fread(ret, 1, len, fp); //close the stream fclose(fp); return ret; } sizedData readFileR(const char *filename){ sizedData ret = {NULL,0}; //open file FILE *fp = fopen(filename, "r+"); if(!fp) return ret; //go to end and get length fseek(fp, 0, SEEK_END); long len = ftell(fp); //allocate enough memory for the data on the heap ret.data = malloc(len); //go back to the beginning and read everything fseek(fp, 0, SEEK_SET); fread(ret.data, 1, len, fp); //set length of data ret.length=len; //close the stream fclose(fp); return ret; } int writeFileC(const char *filename, char *data){ //open file FILE *fp = fopen(filename, "w+"); if(!fp) return -1; //write the data fwrite(data, sizeof(char), strlen(data), fp); //close the stream fclose(fp); return 0; } int writeFileR(const char *filename, sizedData data){ //open file FILE *fp = fopen(filename, "w+"); if(!fp) return -1; //write the data fwrite(data.data, 1, data.length, fp); //close the stream fclose(fp); return 0; } void crash(char *reason){ PRINTE(reason); exit(EXIT_FAILURE); }