Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6232189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:04:26+00:00 2026-05-24T10:04:26+00:00

I’m trying to load an image file into a buffer in order to send

  • 0

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

//imgload.cpp
#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:");
    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;
}

> output

c:\Users\Desktop>imgload
-Enter a file to open:img.gif
-3491 + 10 bytes allocated
buffer size is now: 9


c:\Users\Desktop>imgload
-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?.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:04:28+00:00Added an answer on May 24, 2026 at 10:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.