本文发表在 rolia.net 枫下论坛I give you whole code. try to compiler in your PC.
#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>
#include <conio.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 istream &operator>>(istream &in, Car &C)
{
fflush(stdin);
cout<<"\nCar Model => ";
in.getline(C.model,30);
/*
fflush(stdin);
cout<<"Owner's name => ";
in.getline(C.name,30);
fflush(stdin);
cout<<"Telephone Number => ";
in.getline(C.phone,30);
fflush(stdin);
cout<<"Service Descritption => ";
in.getline(C.description,30);
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;
*/
return in;
}
friend ostream &operator<<(ofstream &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<<C.model<<" ";
return out;
}
}; //class
void 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::app);
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::in);
if (!file2)
{
cerr<<"Error in opening file for input!";
exit(1);
}
//Car temp;
Car *temp = new Car[x];
cout<<"read file";
for (int i=0; i<x; i++)
{
file2.read((char *)&temp, sizeof(Car));
cout<<(i+1)<<"Car="<<temp;
//temp++;
//cout<<*temp;
//cout<<(i+1)<<"Car=";
}
file2.close();
delete []pcars;
// delete []temp;
getch();
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>
#include <conio.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 istream &operator>>(istream &in, Car &C)
{
fflush(stdin);
cout<<"\nCar Model => ";
in.getline(C.model,30);
/*
fflush(stdin);
cout<<"Owner's name => ";
in.getline(C.name,30);
fflush(stdin);
cout<<"Telephone Number => ";
in.getline(C.phone,30);
fflush(stdin);
cout<<"Service Descritption => ";
in.getline(C.description,30);
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;
*/
return in;
}
friend ostream &operator<<(ofstream &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<<C.model<<" ";
return out;
}
}; //class
void 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::app);
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::in);
if (!file2)
{
cerr<<"Error in opening file for input!";
exit(1);
}
//Car temp;
Car *temp = new Car[x];
cout<<"read file";
for (int i=0; i<x; i++)
{
file2.read((char *)&temp, sizeof(Car));
cout<<(i+1)<<"Car="<<temp;
//temp++;
//cout<<*temp;
//cout<<(i+1)<<"Car=";
}
file2.close();
delete []pcars;
// delete []temp;
getch();
}更多精彩文章及讨论,请光临枫下论坛 rolia.net