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

  • SEARCH
  • Home
  • 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 6572363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:59:44+00:00 2026-05-25T14:59:44+00:00

I want to read a raw file, which has 3 interleaving and has a

  • 0

I want to read a raw file, which has 3 interleaving and has a size of about(3.5MB) large into a three dimensional array.. the code that I am using to read the file is:

ifile.open(argv[2], 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++){
            Imagedata1[i][j][k]=ifile.get();
        }
    }
}

The thing is this array is not what i expect it to be.. I need the 1278 to be the width of the image.. the 968 to be the height and 3 bytes are the RGB values.. how should i write the code to read from the file such that the array gets populated correctly.. Thanks.

  • 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-25T14:59:45+00:00Added an answer on May 25, 2026 at 2:59 pm

    First, image files are usually stored, in order of smallest jump to largest, color values, column, row order. You are not reading them in that order.

    ifile.open(argv[2], ios::in|ios::binary);
    for(int j=0; j<968; j++){
        for(int i=0; i<1278; i++){
            for(int k=0; k<3; k++){
                Imagedata1[i][j][k]=ifile.get();
            }
        }
    }
    

    That is how the loop should be arranged, though you may want to rename your variables to keep things straight:

    ifile.open(argv[2], ios::in|ios::binary);
    for(int row=0; row<968; row++){
        for(int col=0; col<1278; col++){
            for(int color=0; color<3; color++){
                Imagedata1[col][row][color]=ifile.get();
            }
        }
    }
    

    Secondly, the way you allocate your array is really broken and inefficient. Here is how it should work:

    #include <iostream>
    #include <fstream>
    
    class ColorValue {
     public:
       ColorValue(unsigned char r, unsigned char g, unsigned char b)
         : r_(r), g_(g), b_(b) {}
       ColorValue() : r_(0), g_(0), b_(0) {}
    
       unsigned char getR() const { return r_; }
       unsigned char getG() const { return g_; }
       unsigned char getB() const { return b_; }
    
     private:
       unsigned char r_, g_, b_;
    };
    
    void readrows(const char *fname, ColorValue imagedata[][1278])
    {
       ::std::ifstream ifile;
       ifile.open(fname, ::std::ios::in|::std::ios::binary);
       for (int row = 0; row < 968; ++row) {
          for (int col = 0; col < 1278; ++col) {
             char r, g, b;
             ifile.get(r);
             ifile.get(g);
             ifile.get(b);
             imagedata[row][col] = ColorValue(r, g, b);
          }
       }
    }
    
    int main(int argc, const char *argv[])
    {
       ColorValue (*imagedata)[1278] = new ColorValue[968][1278];
       readrows(argv[1], imagedata);
       delete[] imagedata;
    }
    

    Using a class for ColorValue keeps you from having magic indexes everywhere in your code for the ‘r’, ‘g’, and ‘b’ components. And allocating the array in this way keeps all the memory used for the image contiguous and removes several levels of unnecessary indirection. These both have the property of making your program much more cache friendly.

    I also found a nice article that’s a really comprehensive treatment of multi-dimensional arrays in C++.

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

Sidebar

Related Questions

I want to read from the text file which is placed in res/raw/text i
I want to read line n1->n2 from file foo.c into the current buffer. I
I have a text file in my res/raw directory. I want to read the
i want to read the text from the file in raw directory(rules.txt) file and
I'm reading in a NetCDF file and I want to read in each array
I have a text file. I want read that file. But In that if
I want to read an xml file, apply a transform, then write to another
I want to read each line from a text file and store them in
There's say some ImageView object. I want to read bits/raw data of this object
I am creating some graphs which I want to update into a database table.

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.