0

所以现在我有一个名为“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

我想在 Customer 类文件中重载 >> 运算符,以便它正确读取所有相关字段,从这些字段实例化 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 的每个部分并将其转换为可以调用 Date 构造函数的形式?例如:

兑换:

14/07/13

至:

Date(14,7,13)
4

0 回答 0