I have a server.conf file, that contains the path to an html.file, with a simple paragraph in it.
I program with a function readFile() that is supposed to take in a file path and return its contents. (The 3 mentioned files are in the same folder).
When I pass in the first file path (server.conf) it works, and the path "index.html" gets returned.
However, when I try to use the same method to read the path that its returned the first time, the file pointer in readFile() is 'nil' after the fopen() file is called.
Any help on why this is happening would be much appreciated.
- The main program:
#include <string.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define MAX_BUFFER 1024
char *readFile(char *filePath){
FILE* fp = malloc(MAX_BUFFER);
char* fileContent = malloc(MAX_BUFFER);
fp = fopen(filePath, "r");
printf("filepath is: %s\n", filePath);
printf("pointer is: %p\n", fp);
if (NULL == fp) {
printf("file can't be opened \n");
}
while (fgets(fileContent, MAX_BUFFER, fp) != NULL) {
printf("content of fileContent in readFile: %s\n", fileContent);
}
fclose(fp);
return fileContent;
}
int main ( void )
{
char *serverConfContent;
serverConfContent = readFile("server.conf");
char *htmlContent = readFile(serverConfContent);
}
- server.conf:
index.html
- index.html:
<html><head><p>This is an example paragraph.</p></html>