Read binary file

This is a discussion on Read binary file within the C++ Programming forums, part of the General Programming Boards category; I have a problem in reading binary file. When i count the number of record, it is correct. But when ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,746
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    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, 05:27 PM
  2. Read in binary file ( pointers )
    By Giant in forum C Programming
    Replies: 41
    Last Post: 06-23-2005, 05: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, 04: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

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