In: Computer Science
car.txt
0001|Mercury|LeSabre|2005|Brown|11000|2019-09-01
0002|Chevrolet|Aveo|2013|Blue|12000|2019-09-02
0003|Datsun|240Z|1979|Orange|13000|2019-09-03
0004|Ford|Galaxie|1964|Black|14000|2019-09-04
0005|Porsche|Boxster|2014|Green|15000|2019-09-05
0006|Honda|Insight|2007|Silver|16000|2019-09-06
1) Design and implement a class of "car" with constructor and destructor, with member variable for each field, and method for get and set each field. (2) Read the file (car.txt) to instantiate each object for each record read from the file. (3) After the file input is done, then display each object to the console.
Car List
Record #1
(1) carID=0001
(2) carManufacturer=Mercury
(3) carModel=LeSabre
(4) carYear=2005
(5) carColor=Brown
(6) carPrice=11000
(7) carInventoryDate=2019-09-01
and so on.
Task#2. Output each object to a file (car1.txt) with the same format as shown in Task#1.
Car List
Record #1
(1) carID=0001
(2) carManufacturer=Mercury
(3) carModel=LeSabre
(4) carYear=2005
(5) carColor=Brown
(6) carPrice=11000
(7) carInventoryDate=2019-09-01
// sample c++ code for object to binary file IO.
// try c++ to compile this program
#include <iostream>
#include <fstream>
using namespace std;
class Student
{
public:
char name[10];
char address[10];
char Gender;
char DOB[10];
Student()
{}
};
int main()
{
cout<<"\n Writting on file: Student1.txt \n";
Student *p=new Student;
cout<<1<<": ";
cin>>p->name;
cout<<"\n";
// to append use ios::app
// ofstream osfile("Student1.txt",ios::binary|ios::app);
ofstream osfile("Student1.txt",ios::binary);
osfile.write((char*)p,sizeof(Student));
osfile.close();
cout<<"\n reading Student1.txt \n";
Student *p2=new Student;
ifstream isfile("Student1.txt",ios::binary);
isfile.read((char*)p2,sizeof(Student));
isfile.seekg(0);
isfile.close();
cout<<1<<": ";
cout<<p2->name;
cout<<"\n";
return 0;
}
The code is as below.
//sample c++ code for object to binary file IO.
// try c++ to compile this program
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
class Car {
public:
string carID;
string carManufacturer;
string carModel;
int carYear;
string carColor;
int carPrice;
string carInventoryDate;
Car(){}
~Car(){}
void setCarID(string val){
carID = val;
}
string getCarID(){
return carID;
}
void setCarManufacturer(string val){
carManufacturer = val;
}
string getCarManufacturer(){
return carManufacturer;
}
void setCarModel(string val){
carModel = val;
}
string getCarModel(){
return carModel;
}
void setCarYear(int val){
carYear = val;
}
int getCarYear(){
return carYear;
}
void setCarColor(string val){
carColor = val;
}
string getCarColor(){
return carColor;
}
void setCarPrice(int val){
carPrice = val;
}
int getCarPrice(){
return carPrice;
}
void setCarInventoryDate(string val){
carInventoryDate = val;
}
string getCarInventoryDate(){
return carInventoryDate;
}
};
int main()
{
Car carArray[20];
int i = 0;
int j = 0;
int tokenNo = 0;
string line;
ifstream myfile ("car.txt");
string tokens[7];
ofstream outfile;
outfile.open ("car1.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
string s = line;
string delimiter = "|";
size_t pos = 0;
string token;
j = 0;
while ((pos = s.find(delimiter)) != string::npos) {
token = s.substr(0, pos);
//cout << token << endl;
tokens[j++] = token;
s.erase(0, pos + delimiter.length());
}
//cout << ":"<<s<<":" << endl;
tokens[j] = s;
carArray[i].setCarID(tokens[0]);
carArray[i].setCarManufacturer(tokens[1]);
carArray[i].setCarModel(tokens[2]);
carArray[i].setCarYear(stoi(tokens[3]));
carArray[i].setCarColor(tokens[4]);
carArray[i].setCarPrice(stoi(tokens[5]));
carArray[i].setCarInventoryDate(tokens[6]);
i++;
cout << "Record #" << i << endl
<< "(1) carID=" << tokens[0] << endl
<< "(2) carManufacturer=" << tokens[1] <<
endl
<< "(3) carModel=" << tokens[2] << endl
<< "(4) carYear=" << tokens[3] << endl
<< "(5) carColor=" << tokens[4] << endl
<< "(6) carPrice=" << tokens[5] << endl
<< "(7) carInventoryDate=" << tokens[6] << endl
<< endl;
outfile << "Record #" << i << endl
<< "(1) carID=" << tokens[0] << endl
<< "(2) carManufacturer=" << tokens[1] <<
endl
<< "(3) carModel=" << tokens[2] << endl
<< "(4) carYear=" << tokens[3] << endl
<< "(5) carColor=" << tokens[4] << endl
<< "(6) carPrice=" << tokens[5] << endl
<< "(7) carInventoryDate=" << tokens[6] << endl
<< endl;
}
myfile.close();
outfile.close();
}
else cout << "Unable to open file";
return 0;
}
Sample output: