#include #include #include "file_helpers.h" char* file_to_string(char* filename) { FILE* fp = fopen(filename,"r"); fseek(fp,0,SEEK_END); long file_size = ftell(fp); rewind(fp); char* buffer = calloc(sizeof(char),file_size+1); fread(buffer,1,file_size,fp); rewind(fp); fclose(fp); return buffer; } /* Finds the number of lines in a file (Which MUST end in a new-line, if the last line is to be counted) */ int num_of_lines(char* filename) { int num_lines = 0; FILE* fp = fopen(filename,"r"); if (fp == NULL) { return -1; } for (int c = getc(fp); c != EOF; c = getc(fp)) { if (c == '\n') { num_lines++; } } rewind(fp); fclose(fp); return num_lines; }