本文发表在 rolia.net 枫下论坛#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>
using namespace std;
class Car
{
char model[30];
char name[20];
char phone[20];
char description[50];
//char date_recent[10];
int year_recent;
int month_recent;
int day_recent;
int number_service;
int year_next;
int month_next;
int day_next;
public:
Car()
{
strcpy(model, " ");
strcpy(name, " ");
strcpy(phone, " ");
strcpy(description, " ");
//strcpy(date_recent, " ");
year_recent=0;
month_recent=0;
day_recent=0;
year_next=0;
month_next=0;
day_next=0;
};
friend ostream &operator << (ostream &out, Car C)
{
//out<<C.model<<" "<<C.name<<" "<<C.phone<<" "<<C.description<<" "<<C.year_recent<<" "<<C.month_recent<<" "<<C.day_recent<<" "<<C.number_service<<" "<<C.year_next<<" "<<C.month_next<<" "<<C.day_next<<endl;
out << "Car " << C.model << " " << C.name << "\n";
return out;
};
friend istream &operator >> (istream &in, Car &C)
{
cout<<"\nCar Model => ";
//in.getline(C.model,30);
in>>C.model;
cout<<"Owner's name => ";
//in.getline(C.name,30);
in>>C.name;
fflush(stdin);
#if 0
fflush(stdin);
cout<<"Telephone Number => ";
in>>C.phon;
fflush(stdin);
cout<<"Service Descritption => ";
in>>C.description;
cout<<"Year of the most recent service => ";
in>>C.year_recent;
cout<<"Month of the most recent service => ";
in>>C.month_recent;
cout<<"Day of the most recent service => ";
in>>C.day_recent;
cout<<"Number of Services Per Year => ";
in>>C.number_service;
cout<<"Year of the next service => ";
in>>C.year_next;
cout<<"Month of the next service => ";
in>>C.month_next;
cout<<"Day of the next service => ";
in>>C.day_next;
#endif
return in;
};
}; //class
int
main()
{
int x;
cout<<"\n Number of Cars => ";
cin>>x;
Car *pcars = new Car[x];
if (!pcars)
{
cout<<"\nMemory Allocation Error!";
exit(1);
}
for (int i=0; i<x; i++)
cin>>pcars[i];
ofstream file1("CARS.DAT", ios::binary | ios::out);
//ofstream file1("CARS.DAT", ios::app);
if (!file1)
{
cerr<<"Error in opening file for output!";
exit(1);
}
file1.write((char *)pcars, sizeof(Car)*x);
file1.close();
//ifstream file2("CARS.DAT");
ifstream file2("CARS.DAT", ios::binary | ios::in);
if (!file2)
{
cerr<<"Error in opening file for input!";
exit(1);
}
#if 1
//Car temp;
Car *temp = new Car[x];
cout<<"read file"<<"\n";
file2.read((char *)temp, sizeof(Car)*x);
for (int i=0; i<x; i++) {
cout<< i+1 <<" : " << temp[i] << "\n";
}
delete []temp;
#endif
file2.close();
delete []pcars;
return 0;
}
运行结果
Administrator@MAX-20121108DXF ~/test
$ g++ test.cpp
Administrator@MAX-20121108DXF ~/test
$ ./a
Number of Cars => 2
Car Model => s
Owner's name => s
Car Model => d
Owner's name => d
read file
1 : Car s s
2 : Car d d更多精彩文章及讨论,请光临枫下论坛 rolia.net
#include <fstream>
#include <cstring>
#include <stdio.h>
using namespace std;
class Car
{
char model[30];
char name[20];
char phone[20];
char description[50];
//char date_recent[10];
int year_recent;
int month_recent;
int day_recent;
int number_service;
int year_next;
int month_next;
int day_next;
public:
Car()
{
strcpy(model, " ");
strcpy(name, " ");
strcpy(phone, " ");
strcpy(description, " ");
//strcpy(date_recent, " ");
year_recent=0;
month_recent=0;
day_recent=0;
year_next=0;
month_next=0;
day_next=0;
};
friend ostream &operator << (ostream &out, Car C)
{
//out<<C.model<<" "<<C.name<<" "<<C.phone<<" "<<C.description<<" "<<C.year_recent<<" "<<C.month_recent<<" "<<C.day_recent<<" "<<C.number_service<<" "<<C.year_next<<" "<<C.month_next<<" "<<C.day_next<<endl;
out << "Car " << C.model << " " << C.name << "\n";
return out;
};
friend istream &operator >> (istream &in, Car &C)
{
cout<<"\nCar Model => ";
//in.getline(C.model,30);
in>>C.model;
cout<<"Owner's name => ";
//in.getline(C.name,30);
in>>C.name;
fflush(stdin);
#if 0
fflush(stdin);
cout<<"Telephone Number => ";
in>>C.phon;
fflush(stdin);
cout<<"Service Descritption => ";
in>>C.description;
cout<<"Year of the most recent service => ";
in>>C.year_recent;
cout<<"Month of the most recent service => ";
in>>C.month_recent;
cout<<"Day of the most recent service => ";
in>>C.day_recent;
cout<<"Number of Services Per Year => ";
in>>C.number_service;
cout<<"Year of the next service => ";
in>>C.year_next;
cout<<"Month of the next service => ";
in>>C.month_next;
cout<<"Day of the next service => ";
in>>C.day_next;
#endif
return in;
};
}; //class
int
main()
{
int x;
cout<<"\n Number of Cars => ";
cin>>x;
Car *pcars = new Car[x];
if (!pcars)
{
cout<<"\nMemory Allocation Error!";
exit(1);
}
for (int i=0; i<x; i++)
cin>>pcars[i];
ofstream file1("CARS.DAT", ios::binary | ios::out);
//ofstream file1("CARS.DAT", ios::app);
if (!file1)
{
cerr<<"Error in opening file for output!";
exit(1);
}
file1.write((char *)pcars, sizeof(Car)*x);
file1.close();
//ifstream file2("CARS.DAT");
ifstream file2("CARS.DAT", ios::binary | ios::in);
if (!file2)
{
cerr<<"Error in opening file for input!";
exit(1);
}
#if 1
//Car temp;
Car *temp = new Car[x];
cout<<"read file"<<"\n";
file2.read((char *)temp, sizeof(Car)*x);
for (int i=0; i<x; i++) {
cout<< i+1 <<" : " << temp[i] << "\n";
}
delete []temp;
#endif
file2.close();
delete []pcars;
return 0;
}
运行结果
Administrator@MAX-20121108DXF ~/test
$ g++ test.cpp
Administrator@MAX-20121108DXF ~/test
$ ./a
Number of Cars => 2
Car Model => s
Owner's name => s
Car Model => d
Owner's name => d
read file
1 : Car s s
2 : Car d d更多精彩文章及讨论,请光临枫下论坛 rolia.net