Preface: I am just engaging C, so forgive my incompetence. The cause of this problem is probably basic.
Problem: I am trying to read a file and serve it over http with a socket. For some reason, the printf output of a previously read file varies depending on how much of the subsequent code is included.
Here is my code.
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
int main()
{
    // open a file to serve
    FILE *file;
    file = fopen("index.html", "r");
    if (file == NULL)
    {
        printf("Failed to open file.");
        exit(EXIT_FAILURE);
    }
    // Get file content
    char file_content[1024];
    if (fgets(file_content, 1024, file) != NULL)
    {
        fclose(file);
        // Add header to file content
        char http_header[2048] = "HTTP/1.1 200 OK\r\n\n";    
        strncat(http_header, file_content, 1028);
        // This output varies depending on inclusion of proceeding code.
        printf("%s", http_header);
        // create a socket
        int server_socket;
        server_socket = socket(AF_INET, SOCK_STREAM, 0);        
        // define the address
        struct sockaddr_in server_address;
        server_address.sin_family = AF_INET;
        server_address.sin_port = htons(8001);
        server_address.sin_addr.s_addr = INADDR_ANY;
        bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));        
        listen(server_socket, 1);
        int client_socket;        
        while (1)
        {
            client_socket = accept(server_socket, NULL, NULL);
            send(client_socket, http_header, sizeof(http_header), 0);
            close(client_socket);
        }
        return 0;
    }
}
If I comment out everything past the printf statement, I get this intended output..
HTTP/1.1 200 OK
<html><body>Hi</body></html>
But if I run all the code, I get this instead..
HTTP/1.1 200 OK
If I can improve my question at all, please let me know how. Thank you.
