Reading in a binary file

This is a discussion on Reading in a binary file within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I'm having a problem reading in a binary file. Here's my snippet of code I'm trying to work ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    Reading in a binary file

    Hey guys,

    I'm having a problem reading in a binary file.

    Here's my snippet of code I'm trying to work with


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    static unsigned short read_array[25198325];
    
    int main()
    {
    	//Tag directory of file
    	const char *file1 = "/home/my_name/programs/InputFile.ima";
    
    	//Set input file reading
    	fstream reader(file1, ios::in | ios::binary);
    
    	if (!reader)
    	{
    		cout << "\nFile doesn't exist\n" << endl;
    		return 0;
    	}
    
    	//char ch;
    	
    	istream &read(read_array, 25198320);  
    	//reader.read(read_array, 25198320);  
    
    	return 0;
    
    }
    I'm trying to get help out of some books and some online help sites, but haven't gotten anywhere with those.
    (i.e. I have no idea what I'm doing )

    So, I'm trying to just read in data from a 50 MB binary file, and to store all the data in the array before I do anything else.

    I get this with the code above
    error: invalid initialization of non-const reference of
    type 'std::istream&' from a temporary of type 'int'

    I've tried to change the array to type char, but that hasn't worked either. It really doesn't like that istream &read line. Other than that, the file initialization part seems fine.

    Anyone have any ideas? That would help alot

    Thanks,
    Bri Rock

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    istream &read(read_array, 25198320);
    I don't like the looks of that! You already have a fstream object right? Well use that to read.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > //reader.read(read_array, 25198320);

    Try this:
    reader.read(reinterpret_cast<char *> (read_array), 25198320);

    Or:
    reader.read(reinterpret_cast<char *> (read_array), sizeof(read_array));

    Also, you can use gcount() to find out how many bytes were read:

    cout << "bytes read: " << reader.gcount() << endl;


    EDIT: Changed static_cast to reinterpret_cast. I have no idea what the differences are between static_cast, reinterpret_cast, and dynamic_cast. Except dynamic_cast occurs at runtime perhaps?
    Last edited by swoopy; 07-08-2004 at 02:06 AM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>50 MB binary file,
    >>and to store all the data in the array
    That's a fair sized amount of data to read in one go. You'd do better by reading chunks at a time, imho.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    static unsigned char     read_array[25198325];
    
    int main(void)
    {
      const char  *file1 = "junk1.cpp";
    
      fstream     reader(file1, ios::in | ios::binary);
    
      if (!reader)
      {
        cout << "\nFile doesn't exist\n" << endl;
        return(0);
      }
    
      reader.read(read_array, 25198320);
      cout << read_array;
      reader.close();
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 09:22 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  3. Problem reading a delimited file into a binary tree
    By neolyn in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2004, 06:51 PM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 01:00 PM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21