C-Webserver/src/fileReader.c

40 lines
720 B
C

#include <stdio.h>
#include <stdlib.h>
#include "../include/fileReader.h"
char *ReadHTML(char path[]) {
FILE *file_ptr = fopen(path, "r");
int fileSize = GetFilesize(file_ptr);
int i = 0;
char *file = malloc(fileSize + 1);
char ch;
if (NULL == file_ptr) {
printf("File can't be opened \n");
}
while ((ch = fgetc(file_ptr)) != EOF) {
file[i] = ch;
i++;
}
file[i] = '\0';
fclose(file_ptr);
return file;
}
int GetFilesize(FILE *file_ptr) {
int prev = ftell(file_ptr);
fseek(file_ptr, 0L, SEEK_END);
int size = ftell(file_ptr);
fseek(file_ptr, prev, SEEK_SET);
return size;
}