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.
| |
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:
| |
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
| |
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...
'
| |
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 ?!!!
| |
Last edited on
@OP you lot said that this doesn't work:
| |
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:
| |
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.
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