Customer.txt内容如下
1000000001,1,Zhang,San,Xingfudajie 4-21,New York,Youta,aaa,123456789
1000000002,1,Zhou,San,Xingfudajie 5-21,New York,Youta,aaa,223456789
1000000003,1,Wu,San,Xingfudajie 6-21,New York,Youta,aaa,323456789
1000000004,1,Zheng,San,Xingfudajie 7-21,New York,Youta,aaa,423456789
1000000005,1,Wang,San,Xingfudajie 8-21,New York,Youta,aaa,523456789
1000000006,1,Liu,San,Xingfudajie 4-44,New York,Youta,aaa,623456789
1000000007,1,Tang,San,Xingfudajie 4-23,New York,Youta,aaa,723456789
1000000008,1,Han,San,Xingfudajie 4-18,New York,Youta,aaa,823456789
1000000009,1,Dong,San,Xingfudajie 3-22,New York,Youta,aaa,923456789
1000000010,1,Gao,Yi,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000011,1,Gao,Yang,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000012,1,Gao,Hao,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000013,1,Gao,Tian,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000014,1,Gao,Xiang,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000015,1,Gao,Mei,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000016,1,Gao,Li,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000017,1,Gao,Huan,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000018,1,Gao,Juan,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000019,1,Gao,Ya,Xingfudajie 7-21,New York,Youta,aaa,103456789
1000000020,1,Gao,Song,Xingfudajie 7-21,New York,Youta,aaa,103456789
代码如下:
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
using namespace std;
class People
{
private:
string Lastname;
string Firstname;
string Address;
string City;
string State;
string Zip;
string Phone;
public:
People(){}
People(string ln,string fn,string addr, string ct,string state,string zip,string phone)
{
Lastname = ln;
Firstname = fn;
Address = addr;
City = ct;
State = state;
Zip = zip;
Phone = phone;
}
~People(){}
//setter
void setLastName(string ln){Lastname = ln;}
void setFirstName(string fn){Firstname = fn;}
void setAddress(string a){Address = a;}
void setCity(string ct){City = ct;}
void setState(string st){State = st;}
void setZip(string zip){Zip = zip;}
void setPhone(string ph){Phone = ph;}
//getter
string getLastName(){return Lastname;}
string getFirstName(){return Firstname;}
string getAddress(){return Address;}
string getCity(){return City;}
string getState(){return State;}
string getZip(){return Zip;}
string getPhone(){return Phone;}
};
class Customer : public People
{
private:
string CustomerNmb;
bool MailList;
public:
Customer(){}
Customer(string ln,string fn,string addr, string ct,string state,string zip,string phone,string nmb,bool b):People(ln,fn,addr,ct,state,zip,phone)
{
CustomerNmb = nmb;
MailList = b;
}
~Customer(){}
//setter
void setCustomNmb(string nmb){CustomerNmb = nmb;}
void setMailList(bool b){MailList = b;}
//getter
string getCustomNmb()
{
if (CustomerNmb.empty())
{
//自动生成一个Custom Number
char buf[11]={0};
srand((unsigned int)time(0));
for (int i = 0;i<10;i++)
{
buf[i] = '0'+ rand()%10;
}
CustomerNmb = buf;
}
return CustomerNmb;
}
bool getMailist(){return MailList;}
friend istream & operator>>(istream &is,Customer &c)
{
char buf[200] = {0};
is.getline(buf,200);//读入一行数据
string s = buf;
int startpos = s.find(",",0);
if(startpos < 0) return is;
//第一个元素Custom nmb
c.setCustomNmb(s.substr(0,startpos));
//第二个元素是否在列表中
int endpos = s.find(",",startpos+1);
if(endpos < 0) return is;
startpos +=1;
string blist = s.substr(startpos,endpos - startpos);
startpos = endpos;
if (blist.compare("1")==0)
c.setMailList(true);
else
c.setMailList(false);
//第三个元素Lastname
endpos = s.find(",",startpos+1);
if(endpos < 0) return is;
startpos +=1;
c.setLastName(s.substr(startpos,endpos - startpos));
startpos = endpos;
//第四个元素
endpos = s.find(",",startpos+1);
if(endpos < 0) return is;
startpos +=1;
c.setFirstName(s.substr(startpos,endpos - startpos));
startpos = endpos;
//第五个元素address
endpos = s.find(",",startpos+1);
if(endpos < 0) return is;
startpos +=1;
c.setAddress(s.substr(startpos,endpos - startpos));
startpos = endpos;
//第六个元素city
endpos = s.find(",",startpos+1);
if(endpos < 0) return is;
startpos +=1;
c.setCity(s.substr(startpos,endpos - startpos));
startpos = endpos;
//第七个元素state
endpos = s.find(",",startpos+1);
if(endpos < 0) return is;
c.setState( s.substr(startpos,endpos - startpos));
startpos = endpos;
//第八个元素zip
endpos = s.find(",",startpos+1);
if(endpos < 0) return is;
startpos +=1;
c.setZip(s.substr(startpos,endpos - startpos));
startpos = endpos;
//第9个元素phone
startpos +=1;
endpos = s.length();
if(endpos < 0) return is;
c.setPhone(s.substr(startpos,endpos - startpos));
return is;
}
void display()
{
cout << CustomerNmb << "," << MailList << "," << getLastName() <<","<<getFirstName()<< ",";
cout << getAddress()<<"," << getCity() << "," << getState()<<"," << getZip() << "," << getPhone() << endl;
}
};
//读文件
Customer* ReadFile(const char* name,int &nmb)
{
int size= 40;
nmb = 0;
Customer* a = new Customer[size]; //申请40个空间
ifstream is(name);
if (!is.is_open())
{
cout << "文件打开失败" << endl;
return 0;
}
while(!is.eof())
{
Customer cus;
is >> cus;
if(nmb >= size)
{
size = size * 2;
Customer* pt = new Customer[size];
for (int i=0;i< nmb;i++)
{
pt[i] = a[i];
}
delete[] a;
a = pt;
}
a[nmb++] = cus;
}
is.close();
return a;
}
int main()
{
int nmb = 0;
Customer* pp = 0;
pp = ReadFile("Customer.txt",nmb); //读取文件
for (int i = 0; i< nmb;i++)
{
pp[i].display();
}
delete[] pp;
pp = 0;
system("pause");
return 0;
}