现在我有一个名为“customers.txt”的制表符分隔文件,其中每个客户数据库条目具有以下格式:
id<tab>name<tab>address<tab>city<tab>postcode<tab>purchaseDate<tab>amountowed
一些示例条目如下所示:
C0001 George I. Bullock 1295 Donec St. Limena 9545 14/07/13 3548.17
C0002 Kyla M. Byrd Ap #505-6018 Adipiscing Ave Camporotondo di Fiastrone 5773 05/06/14 6934.61
C0003 Moses J. Decker 533-2825 Turpis Rd. Carnoustie 4473 27/01/14 6801.02
我想重载&gt;&gt;我的Customer类文件中的operator,以便它正确读取所有相关字段,从这些字段中实例化Customer对象,然后将新创建的Customer对象存储在Customer向量中。但我不知道如何去做。这就是我到目前为止所做的:
//main.cpp:
vector <Customer> cList;
ifstream cDbase("datafiles/customers.txt");
if (!cDbase)
{
cerr << "Customers.txt could not be read." << endl;
exit(1);
}
if (cDbase.good())
{
while (!cDbase.eof())
{
/*
I HAVE NO IDEA WHAT TO PUT HERE
*/
Customer newCust = Customer(in);
cList.push_back(newCust);
}
cDbase.close();
}
//Customer.h
private:
std::string custID;
std::string custName;
std::string custAddress;
std::string custCity;
int custPCode;
Date purchaseDate;
double amountOwed;
public:
friend std::istream& operator>> (std::istream &in, Customer &cust);
//Customer.cpp:
istream& operator>> (istream &in, Customer &cust)
{
in >> cust.custID;
in >> cust.custName;
in >> cust.custAddress;
in >> cust.custCity;
in >> cust.custPCode;
in >> cust.amountOwed;
return in;
}
就像我说的,我不知道我在做什么,所以任何帮助都会非常感激。
另一个问题是日期字段。如何阅读文本文件中列出的日期的每个部分并将其转换为我可以调用我的Date构造函数的表单?例如:
转换:
14/07/13
为:
Date(14,7,13)