Thread: read binary image w/ fstream?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    read binary image w/ fstream?

    Hi, I have a binary image (128x128x64x8) in float (4 byte). I am trying to read it into a vector w/ fstream. The program appears to open and read the file correctly, but the values displayed are not correct, so I assume I am somehow reading the file incorrectly. The implementation is shown below, any help would be greatly appreciated

    Code:
    void float_Image::read(const char* file_name){
    	ifstream in;
    	in.open(file_name,ios::binary|ios::in);
    	if(in.is_open()) cout<<"reading..."<<endl;
    	else cout<<"file not opened successfully"<<endl;
    	float num;
    	for (unsigned int i=0;i<x_*y_*z_*f_;++i){
    		in>>num;
    		binary_data_.push_back(num);
    	}
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The stream extraction operator is designed to read values seperated by whitespace. If this is a binary file with simply raw data in it, I would doubt that the data is formated as such.

    Your code tries to read data even if the open fails. You should resolve this.

    If the file is a binary file, you should be using the stream's read member function rather than the stream extraction operator (>>). Something like this:
    Code:
    while( in.read( reinterpret_cast<char*>(&num), sizeof(num) ) )
        binary_data_.push_back(num);
    This assumes the file contains the 32Mb (128*128*64*8*4bytes) that you wish to read. If the file is larger (or smaller), it will read as much as is available to read. If the file is larger and you want to stop after the first 32 Mb, then you'd likely need to use a counter and the x_*y_*z_*f_ as in your original code.

    You also might want to consider using the vector's reserve member function ahead of the actual read:
    Code:
    binary_data_.reserve(128*128*64*8);
    You can also check after the read is done to verify that the correct number of float values have been read:
    Code:
    if( binary_data_.size() != 128*128*64*8 )
    {
        // Something bad happened
    }
    else
    {
        // Everything is OK!
    }
    Depending on how the function is called you might want to also empty the vector before reading into it so a previous runs set of data doesn't pollute things.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    Yes, I understand now. Thanks very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using fstream to read from file
    By csteinsv in forum C++ Programming
    Replies: 0
    Last Post: 06-02-2009, 01:52 PM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. efficient binary file read
    By Marv in forum C Programming
    Replies: 15
    Last Post: 11-19-2007, 04:42 PM
  4. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  5. Read binary file
    By Ken JS in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2007, 11:12 AM