Use you project 1 (or similar one that you wrote) with header and cpp in same fi
ID: 3826136 • Letter: U
Question
Use you project 1 (or similar one that you wrote) with header and cpp in same file to write data to a random access (binary) file. Also include a program that reads the file. Please note that instead of string you need to use array of characters. See "writing to binary" and "reading from binary" examples.
This is my Code:
Headfile Data.h
#include <iostream>
#include <string.h>
using namespace std;
class data {
char name1[20], name2[20], name3[20], name4[20], name5[20];
float sal1, sal2, sal3, sal4, sal5;
int h_year1, h_year2, h_year3, h_year4, h_year5;
int year, years[5], i;
public:
void setNames(char na1[], char na2[], char na3[], char na4[], char na5[]) {
strcpy_s(name1, na1);
strcpy_s(name2, na2);
strcpy_s(name3, na3);
strcpy_s(name4, na4);
strcpy_s(name5, na5);
}
void setSalaries(float sa1, float sa2, float sa3, float sa4, float sa5) {
sal1 = sa1;
sal2 = sa2;
sal3 = sa3;
sal4 = sa4;
sal5 = sa5;
}
void setHire_years(int y1, int y2, int y3, int y4, int y5) {
h_year1 = y1;
h_year2 = y2;
h_year3 = y3;
h_year4 = y4;
h_year5 = y5;
}
void year_person_hired() {
year = 2017;
i = 0;
years[i++] = year - h_year1;
years[i++] = year - h_year2;
years[i++] = year - h_year3;
years[i++] = year - h_year4;
years[i++] = year - h_year5;
}
void print_data() {
cout << "Person Name Salary Hire_date Years since person hired" << endl;
cout << name1 << " " << sal1 << " " << h_year1 << " " << years[0] << endl;
cout << name2 << " " << sal2 << " " << h_year2 << " " << years[1] << endl;
cout << name3 << " " << sal3 << " " << h_year3 << " " << years[2] << endl;
cout << name4 << " " << sal4 << " " << h_year4 << " " << years[3] << endl;
cout << name5 << " " << sal5 << " " << h_year5 << " " << years[4] << endl;
}
};
CPP.
#include <iostream>
#include <string.h>
#include "data.h"
using namespace std;
int main()
{
int setNames;
int setSalaries;
int setHire_years;
int DTSETNAMES;
int DTSETSALARIES;
data dt;
char names[5][10];
float sal[5];
int y[5], i;
cout << "Enter Names of 5 Persons" << endl;
for (i = 0; i < 5; i++)
cin >> names[i];
dt.setNames(names[0], names[1], names[2], names[3], names[4]);
cout << "Enter Salaries of 5 Persons" << endl;
for (i = 0; i < 5; i++)
cin >> sal[i];
dt.setSalaries(sal[0], sal[1], sal[2], sal[3], sal[4]);
cout << "Enter Hire_dates of 5 Persons" << endl;
for (i = 0; i < 5; i++)
cin >> y[i];
return 0;
};
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class A
{
public :
int a;
string b;
A(int num , string text)
{
a = num;
b = text;
}
};
int main()
{
A myA(1,"Hello");
A myA2(2,"test");
cout << "Num: " << myA.a<<endl<<"Text: "<<myA.b<<endl;
wofstream output;
output.open("12542.dat" , ios::binary );
if(! output.fail())
{
output.write( (wchar_t *) &myA , sizeof(myA));
cout << "writing done ";
output.close();
}
else
{
cout << "writing failed ";
}
wifstream input;
input.open("12542.dat" , ios::binary );
if(! input.fail())
{
input.read( (wchar_t *) &myA2 , sizeof(myA2));
cout << "Num2: " << myA2.a<<endl<<"Text2: "<<myA2.b<<endl;
cout << "reading done ";
}
else
{
cout << "reading failed ";
}
cin.get();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.