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 6560327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:26:43+00:00 2026-05-25T13:26:43+00:00

I am opening a 3.5 MB file in C reading it into an unsigned

  • 0

I am opening a 3.5 MB file in C reading it into an unsigned char array Image data which I have initialized as follows:

unsigned char *** Imagedata;
Imagedata = new unsigned char**[1278];
for(int i=0; i<1278; i++){
    Imagedata[i]= new unsigned char*[968];
    for(int j=0; j<968; j++){
        Imagedata[i][j]= new unsigned char[3];
    }
}

Now i open the file and read it into the array as:

ifstream ifile;
ifile.open("abcde.raw", ios::in|ios::binary);
for(int i=0; i<1278; i++){
    for(int j=0; j<968; j++){
        for(int k=0; k<3; k++){
            ifile>>Imagedata[i][j][k];
        }
    }
}
ifile.close();

The next step is to just rewrite the bytes into a new file.. which i call rawfile.. I have tried to achieve it like this:

ofstream myfile;
myfile.open("rawfile.raw", ios::out|ios::binary);
for(int i=0; i<1278; i++){
    for(int j=0; j<968; j++){
        myfile.write((char *)Imagedata[i][j],3*sizeof(unsigned char));
    }
}
myfile.close();

It somehow doesn seem to work.. the image file that i get is garbage.. what could be the problem?

  • 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-25T13:26:43+00:00Added an answer on May 25, 2026 at 1:26 pm
    ifile>>Imagedata[i][j][k];
    

    This is formatted input, it eats ‘whitespace’ characters even though you specified ios::binary. Use

    Imagedata[i][j][k] = ifile.get();
    

    Even better, allocate one big chunk of memory for the whole file and read it by one read call. What you do now is allocating a pointer for each pixel, which is very wasteful especially for 64-bit systems.

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

Sidebar

Related Questions

When opening a file from your hard drive into your browser, where is the
I am opening a file which has 100,000 URL's. I need to send an
When I'm opening a new file in Vim and I use tab completion, it
is opening a large file once reading it completely once to list faster (or)
I'm opening a CSV file and reading values from it using File.open(filename). So I
I have following text after reading a file: 06/16/2011 04:01:05 AM : Process Start
I am opening file in binary mode using fopen(abhi.zip,rb); and reading contents from it
I'm looking into extending some code that handles automatic opening and reading of Excel
I'm looking for a way of opening a file for both reading and appending.
We have extracted the jpeg image in the variable char *raw_image. How can we,

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.