Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reading and outputting data from a binary file...

reading and outputting data from a binary file...

Scheduled Pinned Locked Moved C / C++ / MFC
iosdebugginghelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Moochie5
    wrote on last edited by
    #1

    I am having difficulty reading from a binary file. For some reason, the program is not reading from it properly. My code is short and so I will show all of it #include #include struct Info { //unsigned short ID; int ID; float GPA, FinAid; char state[3]; char gender[2]; char Fname[15]; char Lname[15]; char status[15]; }; void main(void) { fstream people; Info person; char again; people.open("C:\\Student.bin", ios::in | ios::binary); if(!people) { cout << "Error Opening File! Program Aborting!!\n\n"; return; } people.read((char *)&person, sizeof(person)); while (!people.eof()) { cout << "ID: "; cout << person.ID << endl; cout << "GPA: "; cout << person.GPA << endl; cout << "Financial Aid: "; cout << person.FinAid << endl; cout <<"State: "; cout << person.state << endl; cout << "Gender: "; cout << person.gender << endl; cout << "First Name: "; cout << person.Fname << endl; cout << "Last Name: "; cout << person.Lname << endl; cout << "Status: "; cout << person.status << endl; cin.get(again); people.read((char *)&person, sizeof(person)); } people.close(); } When I debug it, it will not gather the correct data and also show weird characters. I get the feeling that it is trying to read the file in text file mode still. Not sure why as I've checked examples through the internet and I have pretty much the same code.

    S C 2 Replies Last reply
    0
    • M Moochie5

      I am having difficulty reading from a binary file. For some reason, the program is not reading from it properly. My code is short and so I will show all of it #include #include struct Info { //unsigned short ID; int ID; float GPA, FinAid; char state[3]; char gender[2]; char Fname[15]; char Lname[15]; char status[15]; }; void main(void) { fstream people; Info person; char again; people.open("C:\\Student.bin", ios::in | ios::binary); if(!people) { cout << "Error Opening File! Program Aborting!!\n\n"; return; } people.read((char *)&person, sizeof(person)); while (!people.eof()) { cout << "ID: "; cout << person.ID << endl; cout << "GPA: "; cout << person.GPA << endl; cout << "Financial Aid: "; cout << person.FinAid << endl; cout <<"State: "; cout << person.state << endl; cout << "Gender: "; cout << person.gender << endl; cout << "First Name: "; cout << person.Fname << endl; cout << "Last Name: "; cout << person.Lname << endl; cout << "Status: "; cout << person.status << endl; cin.get(again); people.read((char *)&person, sizeof(person)); } people.close(); } When I debug it, it will not gather the correct data and also show weird characters. I get the feeling that it is trying to read the file in text file mode still. Not sure why as I've checked examples through the internet and I have pretty much the same code.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Where's the code that writes to the file? Regards Senthil My Blog

      M 1 Reply Last reply
      0
      • S S Senthil Kumar

        Where's the code that writes to the file? Regards Senthil My Blog

        M Offline
        M Offline
        Moochie5
        wrote on last edited by
        #3

        Sorry if I wasn't clear. The program does not write to the file but rather reads in the information then outputs it on the screen for the user to see. It does not write to the binary file.

        S 1 Reply Last reply
        0
        • M Moochie5

          Sorry if I wasn't clear. The program does not write to the file but rather reads in the information then outputs it on the screen for the user to see. It does not write to the binary file.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          You can read only information that was previously written. Your program (or some other program) must have written the exact same structure instances to a file. Only then will your code work. Regards Senthil My Blog

          1 Reply Last reply
          0
          • M Moochie5

            I am having difficulty reading from a binary file. For some reason, the program is not reading from it properly. My code is short and so I will show all of it #include #include struct Info { //unsigned short ID; int ID; float GPA, FinAid; char state[3]; char gender[2]; char Fname[15]; char Lname[15]; char status[15]; }; void main(void) { fstream people; Info person; char again; people.open("C:\\Student.bin", ios::in | ios::binary); if(!people) { cout << "Error Opening File! Program Aborting!!\n\n"; return; } people.read((char *)&person, sizeof(person)); while (!people.eof()) { cout << "ID: "; cout << person.ID << endl; cout << "GPA: "; cout << person.GPA << endl; cout << "Financial Aid: "; cout << person.FinAid << endl; cout <<"State: "; cout << person.state << endl; cout << "Gender: "; cout << person.gender << endl; cout << "First Name: "; cout << person.Fname << endl; cout << "Last Name: "; cout << person.Lname << endl; cout << "Status: "; cout << person.status << endl; cin.get(again); people.read((char *)&person, sizeof(person)); } people.close(); } When I debug it, it will not gather the correct data and also show weird characters. I get the feeling that it is trying to read the file in text file mode still. Not sure why as I've checked examples through the internet and I have pretty much the same code.

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            The problem is probably due to the member structure alignement: the data in the file have probably been saved with another program which specified a member struct alignement of 1. Member struct alignement means on which byte boundaries are stored all the fields of your structure in memory. Thus, specifying a member struct of 8 (the default), every fields of the structure start on a 8 byte boundary. When you try to read directly data from a file and 'copy' it in memory, if the structure was not saved with the same member struct alignement, this will results in misalignement of your fields (and so, you won't be able to read anything). To avoid the problem, try to set the alignement of your structure to 1: #pragma pack(1) struct Info { //unsigned short ID; int ID; float GPA, FinAid; char state[3]; char gender[2]; char Fname[15]; char Lname[15]; char status[15]; }; #pragma pack // reset to default

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups