I Initiate my string and call my function like this:
int main() {
...
char *fileBuffer;
readFileToBuffer("/tmp/file.txt", &fileBuffer);
...
}
The purpose of this function is to get the contents of file.txt and put it into the fileBuffer variable. Because the content of file.txt is dynamic, I allocate the memory for fileBuffer dynamically in the readFileToBuffer() function like so:
void readFileToBuffer(char *filePath, char **fileBuffer) {
...
FILE *reqFile = fopen(filePath, "r");
fseek(reqFile, 0, SEEK_END);
long fileSize = ftell(reqFile);
fseek(reqFile, 0, SEEK_SET);
*fileBuffer = malloc(fileSize + 1);
fread(fileBuffer, fileSize, 1, reqFile);
fclose(reqFile);
...
}
This is causing a segmentation fault. I've googled around and this seems to be the correct way when allocating memory inside of a function.
Any idea why this is happening?
freadcall again.