读取结构化文件

Reading a structured file

提问人:TosinAl 提问时间:4/10/2018 更新时间:4/10/2018 访问量:64

问:

我写了这个简短的程序来从 txt 文件中读取 s:Order

struct Date
{
    int year;
    int month;
    int day;
};

istream& operator>>(istream& is, Date& d)
{
    int dd, m, y;
    char slash1, slash2;
    is >> dd >> slash1 >> m >> slash2 >> y;
    d.day = dd;
    d.month = m;
    d.year = y;
    return is;
}

class Purchase
{
public:
    string product_name;
    double unit_price;
    int count;
};

istream& operator>>(istream& is, Purchase& p)
{
    string pd;
    double price;
    int cnt;
    is >> pd >> price >> cnt;
    if (!is)
    {
        is.unget();
        is.clear(ios_base::failbit);
        return is;
    }
    p.product_name = pd;
    p.unit_price = price;
    p.count = cnt;
    return is;
}

struct Address
{
    string add;
};

istream& operator>>(istream& is, Address& a)
{
    string s;
    string aa;
    while (true)
    {
        is >> aa;
        s = s + aa + ' ';
        if (s[s.length() - 2] == '.')break;
    }
    a.add = s;
    return is;
}


class Order
{
public:
    string name;
    Address address;
    Date dt;
    vector<Purchase>purch;
};


istream& operator>>(istream& is, Order& o)
{

    string nm;
    Address aa;
    Date dd;

    is >> nm>> aa >> dd;
    if (!is)
    {
        is.unget();
        is.clear(ios_base::failbit);
        return is;
    }
    o.name = nm;
    o.address.add = aa.add;
    o.dt.day = dd.day;
    o.dt.month = dd.month;
    o.dt.year = dd.year;

    for (Purchase pp; is >> pp;)
    {
        o.purch.push_back(pp);
    }
    return is;
}

文本文件的格式如下:

John
3, Apple Street, Lagos.
11/3/2018
Soap    100 2
Cream   250 1
Cheese  50  6

Matthew
10, Orange Street, Milan.   
10/1/2018   
Tissue  50  2
Cookies 10  5
Shirts  500 2
Pen 35  1

在函数中测试此程序时:main

int main()
{
    cout << "Enter input file name: ";
    string input_file;
    cin >> input_file;
    ifstream ifs{ input_file };

    vector<Order>ord;

    while (true)
    {
        Order g;
        if (!ifs)break;
        ifs >> g;
        ord.push_back(g);
    }

    cout << ord[0].name << endl;
    cout << ord[0].address.add << endl;
    cout << ord[0].dt.year << endl;
    cout << ord[0].purch[0].count << endl;
    cout << endl;
}

我发现它只将第一个实例读取到向量中。ifstream 在第二次读取新数据时失败并爆发。因此,在上面的示例文件中,我只能成功读取 John 的订单。现在我被困住了,我需要帮助。谢谢。OrderordifsOrder

C++ C++11 文件- IO IO流

评论

1赞 Joseph D. 4/10/2018
您只打印一个订单(即ord[0])
0赞 TosinAl 4/10/2018
@codekaizer我只是这样,因为这是唯一有效的下标......打印时它说矢量下标超出范围ord[1]
0赞 Igor Tandetnik 4/10/2018
for (Purchase pp; is >> pp;)循环在消耗整个文件之前不会终止。
0赞 TosinAl 4/10/2018
@IgorTandetnik 我也尝试了这个循环来阅读:Purchasewhile (true) { Purchase pp; if (!(is >> pp))break; o.purch.push_back(pp); }
1赞 Igor Tandetnik 4/10/2018
(is >> pp)只会变为(在布尔上下文中)on ,因为您在返回之前已经明确清除了它。支票的洗牌不会改变这一事实。falseeoffailbit

答:

0赞 Kostas 4/10/2018 #1

如果将for (Purchase pp; is >> pp;)

int i = 0;
for (Purchase pp; is >> pp && i++ < 3;)

你可以看到代码运行得很好

但是,这会产生可变购买规模的问题。循环常数的大小应该是多少? 我建议在每个对象末尾使用哨兵,例如数据中的“1”。然后,您可以将循环修改为:

for (Purchase pp; is.peek() != 1 && is >> pp;)

另外,不要忘记在循环后杀死下一个角色(它将是“1”)

char c; is >> c;

0赞 TosinAl 4/10/2018 #2

感谢@GillBates的哨兵想法,@IgorTandetnik在我的代码中暴露了错误的部分。我能够通过在每个订单的开头使用整数索引,然后在 operator>>Order 函数的末尾添加 is.clear() 来纠正这个问题。