Hi all. I have been set a task for my c++ class. I have to create a program to take input about a companys weekly timesheets (Employee ID, hours worked, hourly rate, that sort of thing). I am then to write this information to a binary file which can then be read. The aim is to create payslips from some of the data while having the original binary as a backup.
My problem comes from reading the binary file back and displaying it. When I try to display it I get the first few records but then the output is incorrect and the console crashes. Any input on my problem would be much appreciated.
(Im running code::blocks on windows XP)
What do you mean by "binary file". Can you post your exact assignment?
I ask, because this kind of CSV data is usually stored textually. If you want to keep it in a non-textual database, some additional structure must be applied, which typically splits things up.
If you are interested in serialization of an object containing both textual and non-textual data into a non-textual file, that again requires a little bit of extra work and structure. (Well, maybe not so much "extra work" as "different" work.)
What's acutally happening is the string in times is an object that has pointers to dynamically allocated memory. These are maintained by the object (constructor/destructor/copy and so on) and so you can't simply bit-blit the thing as you end up changing those pointer values, making the internal state of the string invalid.
To fix it, change the date field to a char array as you've done for the name fields.
Hi guys, sorry for late response, my friend 'forced' me to get drunk yesterday.
I have changed the string in times to a char. I am having a problem now trying to use the char date inside my struct to apply to all of the timesheet[i].date. Here is my changed code:
struct times{
int empID;
char lastName[15];
char firstName[15];
int dept;
float hours;
float overTime;
float rate;
char date; //changed to char
};
times timesheet[20];
void Display();
void readFileIntoArrayByRecord();
void getTimeData();
void displayTimeData();
void writeBinary();
string sheetDate;
string filename;
int rows;
int y;
constchar *SDate; //added to convert the string sheetDate (which I use to name the output file)
//to a char to add into my struct
int main()
{
getTimeData();
displayTimeData();
writeBinary();
readFileIntoArrayByRecord();
Display();
}
void getTimeData()
{
cout << "Enter the end of week date (dd.mm.yy: include the full stops)" << endl;
cout << "This will be used for all data on this timesheet" << endl;
cin >> sheetDate;
SDate = sheetDate.c_str(); //string to char conversion
for (int i = 0; i <= 2; i++){
timesheet[i].date = SDate; //problem is here
cout << "Enter employee ID: " << endl;
I'm not exactly sure how to fix it, any input would be appreciated. Thanks!
1. Bug on Line 40 - date is only one char, as declared on Line 9.
2. Bug on Line 91 of your original file - you are only writing out one record.
BTW, this is actually a very good exercise, so learn to do it well. Learn to do it correctly.
Binary files are extremely useful - I use them to read/write out gigabyte-size files in milliseconds that would actually take minutes using their equivalent ASCII.