To Read Data From a Binary File You Create Objects From the Following Classes

Hi all

I'thou trying to read information from a binary file (employees.bin), into a struct. However, I'thou unsure on why my data is not existence read correctly.

                        one
2
3
four
v
vi
7
eight
9
10
11
12
13
14
15
sixteen
17
18
xix
xx
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
forty
41
42
43
44
45
46
47
48
49
l
51
52
53
54
55
56
57
58
59
sixty
61
62
63
64
65
66
67
68
69
70
71
72
                                                  #include <fstream>                          #include <iostream>                          #include <iomanip>                          #include <cord>                          #include <ctime>                          #include <cstring>                          #include <cstdlib>                          #include <conio.h>                                                    // for "getch()"                          using                          namespace                          std;                          const                          int                          MAX = 100;                          struct                          empdata {                          //information members                          char                          firstname[13];                          char                          lastname[13];                          char                          idnumber[12];                          char                          datehired[nine];                          double                          payrate ;                          double                          hoursworked ;                          double                          grosspay; };                          int                          main () {         empdata EmployeeRecord;                          int                          index = 0 , limit = 0;    	fstream labfile; 	labfile.open("testEmployees.bin", ios::in | ios::binary);                          if(labfile.fail()) 	{ 		cout <<                          "Unable to open the data file!!!"                          <<endl; 		cout <<                          "Delight make certain your bin file is in the same location as the program!"<<endl<<endl<<endl; 		organisation("Interruption");                          return                          999;  	}          labfile.seekg(0,std::ios::beg); 	labfile.read(reinterpret_cast<char*>(&EmployeeRecord),                          sizeof(EmployeeRecord));                          while(!labfile.eof()) 	{ **Here im just testing to see                          if                          the correct information went in.         cout<<EmployeeRecord.firstname<< endl; 	    cout<<EmployeeRecord.lastname<< endl; 	    cout<<EmployeeRecord.idnumber<< endl; 	    cout<<EmployeeRecord.datehired<< endl; 	    cout<<EmployeeRecord.payrate<< endl; 	    cout<<EmployeeRecord.hoursworked<< endl;  	    labfile.read(reinterpret_cast<char*>(&EmployeeRecord),                          sizeof(EmployeeRecord)); 	    getch(); 	}      labfile.close(); 	getch();                          return(0); }                                              

And i get:

                Andrew Conyers 219-52-2044 02231988 3.43597e+010<<<<wrong, should be payrate 1.72632e-306<<<<incorrect, should be hours worked                              

The payrate and hoursworked are wrong when reading the starting time tape.
Once I hit enter so that information technology reads the file for the next record, all of the values are off.

Been looking for an answer for a while and oasis't been able to find it.

Whatever assistance is appreciated. Thanks in advance.

this is the format of scientific !!! i dont know what you write in your file and and then what yous read from of but test information technology in begining of your code :

cout.setf(ios::fixed, ios::floatfield);

tested information technology, now I get

34359746702.000000<<payrate
0.000000<<hours worked

BTW, I'm not writing anything to a file, but reading. He asks me to make a struct with the data members: first, terminal, ID, engagement hired ,pay and hours.

Then declare a single object variable named EmployeeRecord and write a loop control construction to read information from a .bin file my professor fabricated, one tape at a fourth dimension, into the employeerecord variable.

Does it have anything to do with atof or cypher terminator?

Also is in that location anything I can read to help, went over the affiliate twice but it only explains how to exercise it the basic way.

I'm not sure if I fully sympathise what y'all're trying to do hither simply I would recommend that rather than trying to read the whole struct in one become you lot should endeavour to take each piece. Then if your struct was something like:

                        1
2
3
four
5
vi
7
viii
9
10
eleven
                                                  struct                          MyStruct {                          int                          i;                          char                          c; }                          //read each piece individually similar this                          int                          readNumber = 0;                          char                          readChar =                          ''; labfile.read( &readNumber,                          sizeof(int) ); labfile.read( &readChar,                          sizeof(char) );                      

maybe I'g missing the point here... promise information technology helps!

Were y'all given the struct definition? Sizes and all?

For your struct def

                        one
2
3
4
five
vi
7
viii
9
ten
                                                  int                          main() { 	size_t a =                          sizeof(empdata); 	size_t b =                          sizeof(char) * (xiii + 13 + 12 + 9) +                          sizeof(double) * 3;  	std::cout << a << std::endl; 	std::cout << b << std::endl;                          return                          0; }                      

produces the following output

Assuming yous haven't overridden the compilers default packing?

Then you are reading one additional byte cf. the members declared in struct.

@ceruleus
These worked one past one for the chars in the struct, but I'm still not getting the correct info for the doubles...
'

                        i
2
3
4
5
6
7
eight
9
10
                                                  labfile.read(reinterpret_cast<char*>(&EmployeeRecord.firstname),                          sizeof(EmployeeRecord.firstname));     labfile.read(reinterpret_cast<char*>(&EmployeeRecord.lastname),                          sizeof(EmployeeRecord.lastname));     labfile.read(reinterpret_cast<char*>(&EmployeeRecord.idnumber),                          sizeof(EmployeeRecord.idnumber));     labfile.read(reinterpret_cast<char*>(&EmployeeRecord.datehired),                          sizeof(EmployeeRecord.datehired));   This didnt piece of work    labfile.read(reinterpret_cast<char*>(&EmployeeRecord.payrate),                          sizeof(EmployeeRecord.payrate));     labfile.read(reinterpret_cast<char*>(&EmployeeRecord.hoursworked),                          sizeof(EmployeeRecord.hoursworked));                      

Yet getting weird info for payrate and hoursworked(looks like three.43597e+010), when information technology should return in values of payrate(999.99) and hoursworked(99.ix)

..............

@andy this is the info he gave united states of america, along with a pdf file that says what he wants united states of america to do in the assignment and a bin file(testemployees.bin).

He said-
"For you information, in case you lot need it, I used the following character counts for the char assortment string values in the structure used to create the binary information file. Information technology should non matter, only I desire you lot to have the details just in case."
showtime name and last name: 12+1 (13) <-- that means a maximum of 12 characters + 1 for the Null
id: 11+one (12)
date hired: 8+ane (ix)
pay rate: float
hours worked: float

And the pdf. file with the consignment says:

"Utilize the structure definition from lab four(that'southward the struct I'1000 using) that represents the employee data type. Declare a Single object variable and proper name information technology employeerecord. Write a looping command construction to read data, one tape at a time, into this employeerecord variable. NOTE: The data file I created is a binary data file(testemployees.bin)."

The assignment asks me to do other stuff after that simply I can't fifty-fifty go the information to read properly into the struct. Been trying to find a solution for days at present but I tin can't figure it out. If not and then i'll just take to wait until afterward a exam tomorrow to ask him nigh information technology, merely i'one thousand a bit worried near something similar this showing up on the examination.

Last edited on

@Technyque (8):
labfile.read(reinterpret_cast<char*>(&EmployeeRecord.firstname), sizeof(EmployeeRecord.firstname));

this code non right because firstname is the accost of beginning block which array be started !! then you tin wrote
reinterpret_cast<char*>(EmployeeRecord.firstname)
or
reinterpret_cast<char*>(&EmployeeRecord.firstname[0])
i dont know only i read in complete reference STL , reinterpret_cast is non good thought then you just write
(char *)&obj is better ! only i dont know why !!
your starting time code dont have any problem but are you sure virtually your file ?!!! if y'all dont know about inside of them , then how do you lot know its truthful ?!!!

                        1
2
3
four
5
6
vii
8
nine
ten
eleven
12
13
xiv
15
sixteen
17
18
19
twenty
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
xl
41
42
43
44
45
                        you can test yourself just debug                          this                          code and see yourself :                          struct                          empdata {                          //data members                          char                          firstname[thirteen];                          char                          lastname[13];                          char                          idnumber[12];                          char                          datehired[ix];                          double                          payrate ;                          double                          hoursworked ;                          double                          grosspay;  	empdata() 	{ 		strcpy(firstname,                          "asdf"); 		strcpy(lastname,                          "fdfdf"); 		strcpy(idnumber,                          ""); 		strcpy(datehired,                          "aaaaa");  		payrate = 123; 		hoursworked = 111; 		grosspay = 0; 	} };                          void                          principal() { 	empdata e;  	empdata rr;                          // just for cheque!!                          // just for check !!!!                                                    rr.grosspay = 111111; 	rr.hoursworked = 0; 	strcpy(rr.firstname,                          "");  	ofstream fout("a.dat", ios::out | ios::binary); 	fout.write((const                          char                          *)&e,                          sizeof( empdata )); 	fout.close();  	ifstream fin("a.dat", ios::in | ios::binary); 	fin.read((char                          *)&rr,                          sizeof( empdata )); 	fin.close(); }                      

Last edited on

@OP you lot said that this doesn't work:

                        i
2
                        labfile.read(reinterpret_cast<char*>(&EmployeeRecord.payrate),                          sizeof(EmployeeRecord.payrate));     labfile.read(reinterpret_cast<char*>(&EmployeeRecord.hoursworked),                          sizeof(EmployeeRecord.hoursworked));                      

That makes sense since you are casting a double to a char, then of course information technology won't work. When using basic types (similar double, int, char, etc...) you should just use a c-style cast:

                        ane
2
                        labfile.read( (double)&EmployeeRecord.payrate,                          sizeof(                          double                          ) );     labfile.read( (double)&EmployeeRecord.hoursworked,                          sizeof(                          double                          ) );                      

edit: also, I edited the to a higher place... I'yard non sure if your 2d parameter to read is valid, utilize sizeof(type) instead

Last edited on

@ahura24

Bingo! I ran your code to create "a.dat", and using my original code it'due south reading all the data from "a.dat" like information technology's supposed to. I guess this means the .bin file that my prof gave us may need to exist fixed?

Thanks for all the help, will marker as solved after today after I talk to him.

I would exist interested to hither what your prof's solution is.

For binary data it is standard practice to write or read a single record (= structure) at a time, equally you were attempting to do earlier on.

Last edited on

would this be by unmarried record? I'chiliad pretty certain he wanted it read like this but i guess ill run across.

labfile.read(reinterpret_cast<char*>(&EmployeeRecord), sizeof(EmployeeRecord));

@ceruleus,
I get: invalid cast from type 'double*' to blazon 'double', when it endeavor it that mode.

I'one thousand thinking it may be something wrong with the .bin file he provided usa with, I'll allow ya'll know what he says.

Last edited on

@Technyque then change the (double)&EmployeeRecord.payrate to (double*)&EmployeeRecord.payrate

The betoken I was trying to become across is that I think you don't need to apply reinterpret_cast or whatsoever of those fancy object casts. You should probably merely be using a c-way cast like the above code. I'one thousand really not sure though can someone step in and correct me/agree with me here?

Final edited on

I am a bit concerned nigh the comment about c-manner versus reinterpret cast

I have just checked Sutter and Alexandrescu (C++ Coding Standard) and it states "Don't employ C-way casts".

Ever!

Merely talked to someone in my form. Never felt similar such a noob in my life lol...

The .bin file skillful, I was supposed to utilise bladder instead of double in my struct. Don't know how I missed that, I even posted the e-mail he sent our form with the directions, which states that those 2 values were float.

Thanks everyone that tried to help and deplorable for wasting your time.

:-)

The advantage of mistakes is that they help to "burn" facts into your mind!

Last edited on

Topic archived. No new replies immune.

sturmplairt.blogspot.com

Source: https://www.cplusplus.com/forum/general/47964/

0 Response to "To Read Data From a Binary File You Create Objects From the Following Classes"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel