You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
698 B
C
52 lines
698 B
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#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);
|
|
|
|
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++;
|
|
}
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return num_lines;
|
|
|
|
}
|