Thread: Read binary file

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    12

    Read binary file

    I have a problem in reading binary file.
    When i count the number of record, it is correct.
    But when i try to display the information of the file, the result is 1x0.
    Hope someone can guide me. Thanks...

    This is part of my code...

    Student aStudent;
    Student *pStudent;
    ifstream ins;
    int byte_size = 0, no_rec = 0;

    ins.open("Students.dat", ios::in | ios::binary);
    ins.seekg(0, ios::end);
    byte_size = ins.tellg();
    no_rec= byte_size / sizeof(Student);
    cout << "Number of records in file is: " << no_rec<<endl;

    pStudent = new Student[no_rec];
    ins.seekg(0, ios_base::beg);
    ins.read((char*)&pStudent, no_rec*sizeof(Student));
    cout << pStudent << endl;
    ins.close();

    Lot of help,
    Ken JS

  2. #2
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    other members would appreciate you if you added a little bit of work by making the code inside the code tags
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cout << pStudent << endl;
    Well you're not going to get a nicely formatted list of all students from this line.
    At best, you'll get some hexadecimal value which is the pointer to your memory.

    Something like
    Code:
    for ( int i = 0 ; i < no_rec ; i++ ) {
      // add other members of the struct/class as appropriate
      cout << pStudent[i].name << endl;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    > cout << pStudent << endl;
    Well you're not going to get a nicely formatted list of all students from this line.
    There's no reason why it can't work, he just needs to write an appropriate operator<<()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Read in binary file ( pointers )
    By Giant in forum C Programming
    Replies: 41
    Last Post: 06-23-2005, 04:54 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM