Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring an image file into a buffer (gif,jpeg etc).
    text
    copied!<p>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</p> <pre><code>//imgload.cpp #include &lt;iostream&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; 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:"); gets(filename); f = fopen(filename,"rb"); if (f == NULL){ printf("\nError opening file.\n"); }else{ fseek(f,0,SEEK_END); file_bytes = ftell(f); fseek(f,0,SEEK_SET); buffer = new char[file_bytes+10]; } if (buffer != NULL){ printf("-%d + 10 bytes allocated\n",file_bytes); }else{ printf("-Could not allocate memory\n"); // Call exit?. } while (c != EOF){ c = fgetc(f); buffer[i] = c; i++; } 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; } </code></pre> <p><strong>> output</strong></p> <pre><code>c:\Users\Desktop&gt;imgload -Enter a file to open:img.gif -3491 + 10 bytes allocated buffer size is now: 9 c:\Users\Desktop&gt;imgload -Enter a file to open:img2.gif -1261 + 10 bytes allocated buffer size is now: 7 </code></pre> <p>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?.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload