Take the 2-minute tour ×

I'm trying to load an image file into a buffer in order to send it through a scket. The problem that I'm having is that the program creates a buffer with a valid size but it does not copy the whole file into the buffer. My code is as follow

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main(int argc,char *argv){

    FILE *f = NULL;
    char filename[80];
    char *buffer = NULL;
    long file_bytes = 0;
    char c = '\0';
    int i = 0;

    printf("-Enter a file to open:");

    f = fopen(filename,"rb");
    if (f == NULL){
        printf("\nError opening file.\n");
        file_bytes = ftell(f);

        buffer = new char[file_bytes+10];


    if (buffer != NULL){
        printf("-%d + 10 bytes allocated\n",file_bytes);
        printf("-Could not allocate memory\n");
        // Call exit?.

    while (c != EOF){
        c = fgetc(f);
        buffer[i] = c;

    c = '\0';                   
    buffer[i-1] = '\0';     // helps remove randome characters in buffer when copying is finished..
    i = 0;      

    printf("buffer size is now: %d\n",strlen(buffer));

    //release buffer to os and cleanup....

    return 0;

> output

-Enter a file to open:img.gif
-3491 + 10 bytes allocated
buffer size is now: 9

-Enter a file to open:img2.gif
-1261 + 10 bytes allocated
buffer size is now: 7

From the output I can see that it's allocating the correct size for each image 3491 and 1261 bytes (i doubled checked the file sizes through windows and the sizes being allocated are correct) but the buffer sizes after supposedly copying is 9 and 7 bytes long. Why is it not copying the entire data?.

share|improve this question
also this is all done in order to implement a simple websrver, I just can't seem to transmit images through this method. All text/html files get transmitted fine using this method. –  silent Aug 8 '11 at 11:11
I'm not sure what type of data HTTP protocol transmits. If it is plain text, you must encode your image in MIME coding. It translates binary data in text symbols, so file can be sent, for example, through E-MAIL. If you want to send picture as binary data, it may be good idea to change Content-Type header. Learn about them more. –  George Gaál Aug 8 '11 at 11:21
The while() loop is borked. fgetc() returns an int, not a char. The way you use it, you cannot properly detect EOF. Ditch strlen(), it is not a string. –  Hans Passant Aug 8 '11 at 11:26

3 Answers 3

up vote 5 down vote accepted

You are wrong. Image is binary data, nor string data. So there are two errors:

1) You can't check end of file with EOF constant. Because EOF is often defined as 0xFF and it is valid byte in binary file. So use feof() function to check for end of file. Or also you may check current position in file with maximal possible (you got it before with ftell()).

2) As file is binary it may contain \0 in middle. So you can't use string function to work with such data.

Also I see that you use C++ language. Tell me please why you use classical C syntax for file working? I think that using C++ features such as file streams, containers and iterators will simplify your program.

P.S. And I want to say that you program will have problems with really big files. Who knows maybe you will try to work with them. If 'yes', rewrite ftell/fseek functions to their int64 (long long int) equivalents. Also you'll need to fix array counter. Another good idea is to read file by blocks. Reading byte by byte is dramatically slower.

share|improve this answer
but I'm opening it in binary mode? ("rb"). –  silent Aug 8 '11 at 11:13
It doesn't make sense. –  George Gaál Aug 8 '11 at 11:14
cheers thank you for you're help. it works. –  silent Aug 8 '11 at 11:29

All this is unneeded and actually makes no sense:

c = '\0';                   
buffer[i-1] = '\0';
i = 0;


Don't use strlen for binary data. strlen stops at the first NUL (\0) byte. A binary file may contain many such bytes, so NUL can't be used.

-3491 + 10 bytes allocated /* There are 3491 bytes in the file. */
buffer size is now: 9 /* The first byte with the value 0. */

In conclusion, drop that part. You already have the size of the file.

share|improve this answer
how would I copy the contents of the image binary data into a buffer then? –  silent Aug 8 '11 at 11:14
@sil3nt You are already copying the data in the buffer via you fgetc loop. –  cnicutar Aug 8 '11 at 11:15
hmm.. I'm still not receiving my images on the browser –  silent Aug 8 '11 at 11:18
@sil3nt Use file_bytes instead of strlen. Post more code / ask a new question. You might need to send a header (Content-Type etc.). –  cnicutar Aug 8 '11 at 11:19
I just tried file_bytes, now I know I'm sending the right amount of bytes. But its still not showing the images on the browser. I'' try Content-Type and see. –  silent Aug 8 '11 at 11:22

You are reading a binary file like a text file. You can't check for EOF as this could be anywhere in the binary file.

share|improve this answer

Your Answer


