0
votes

I'm trying to download a file with wget, then open it up, read it into a buffer and then send it through a socket to a server that is waiting for it. I've successfully been able to download the file and then open it up but then something goes wrong when I try to read it into the buffer. The file that I'm trying to download is 8000ish bytes long but when I write it to the buffer and then run sizeof(fileBuffer) it's only reporting a size of 8 bytes. Here is the code:

    //open the file
    if((downloadedFile = fopen(fileName, "rb"))==NULL){
      perror("Downloaded file open");
    }
    //determine file size
    fseek(downloadedFile, 0L, SEEK_END);
    downloadedFileSize = ftell(downloadedFile);
    fseek(downloadedFile, 0L, SEEK_SET);
    printf("%s %d \n", "downloadedFileSizse = ",downloadedFileSize);

    //send back length of file
    if (send(acceptDescriptor, &downloadedFileSize, sizeof(long), 0) == -1){
      perror("send downloaded file size Length");
      exit(0);
    }

    //start by loading file into the buffer
    char *fileBuffer = malloc(sizeof(char)*downloadedFileSize);
    int bytesRead = fread(fileBuffer,sizeof(char), downloadedFileSize, downloadedFile);
    printf("%s %d \n","bytesRead: ", bytesRead);
    printf("sizeof(fileBuffer) Send back to SS: %d\n",sizeof(fileBuffer));

This is the output I'm getting:

HTTP request sent, awaiting response... 200 OK
Length: 8907 (8.7K) [text/html]
Saving to: âindex.htmlâ

100%[======================================>] 8,907       --.-K/s   in 0s

2013-10-13 09:35:16 (158 MB/s) - âindex.htmlâ saved [8907/8907]

downloadedFileSizse =  8907
bytesRead:  8907
sizeof(fileBuffer) Send back to SS: 8
bytesLeft:  0
n:  8907

When I run the fread it's reading 8907 bytes but for some reason it's not writing them to the buffer correctly.

1

1 Answers

2
votes

fileBuffer is a character pointer, so sizeof will report the size of the character pointer, not the size of the buffer. The return value of fread is the number of bytes written to the buffer so use bytesRead for the size of the buffer.