?????????

what do you mean by
fout.write((char *)&sal, sizeof(sal));

what did "sal" role right after class Salary? What does the above code do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 ofstream fout;
 class Salary
{
float basic,gross,deduction,da,hra,ot,oth,pf,lic,fadv,coops,hdfc,netpay;
int abdys;
public:
void add();
}sal;

void Salary::add()
{employee::add();
cout<<"\n\n\tBasic Pay: ";
cin>>basic;
cout<<"\n\n\tDays Absent: ";
cin>>abdys;
cout<<"\n\n\tOvertime Hours: ";
cin>>oth;
cout<<"\n\n\tLoans and Savings\n";
cout<<"\n\n\tLIC: ";
cin>>lic;
cout<<"\n\n\tHDFC: ";
cin>>hdfc;
cout<<"\n\n\tCo Operative Society: ";
cin>>coops;
calculate();
fout.write((char *)&sal,sizeof(sal));
fout.close();
}
That's writing sal as a binary value to the file rather than converting it to text. Unless there's a specific reason to do so, it's probably not the best thing to do it that way.
Sure it is a good way! Unless there's a specific reason to save your data as text, saving it as binary is quick and easy to load it back... I am being silly though as I usually save program configuration files or just data being used in .xml (tinyxml2) or excel documents (.xlsx)

P.S. You must be opening the file in the 'calculate' class method / function [it isnt clear on which it is] which isn't shown in the code...

Or if your not, that's why it's not writing that to a file:

try adding a:
1
2
3
4
5
6
7
8
9
10
11
12
13
fout.open("C:\\salarydata.bin", std::ofstream::out | std::ofstream::binary);
[code]
before the fout.write line ;)

then use an ifstream to read the file back into &sal something like:

[code]
memset(&sal, 0, sizeof(sal));

ifstream fin;
fin.open("C:\\salarydata.bin", std::ifstream::in | std::ifstream::binary);
fin.read((char*)&sal, sizeof(sal));
fin.close();


you could do that in another instance after you saved it initially and it will load that data back into the program from the file on disk! :)


Last edited on
Sure it is a good way! Unless there's a specific reason to save your data as text, saving it as binary is quick and easy
... and non-portable.
Topic archived. No new replies allowed.