读入包含多个字段的C ++文本文件

时间:2015-07-19 13:52:09

标签: c++

我有一个像这样的文本文件

A: [[1, 95], [78, 110]],
B: [[85, 86], [88, 93], [96, 156], [158, 442]],

如何在c ++中阅读并获得像

这样的cout
Period : A with ranges 1-95, 78-100
Period : B with ranges 85-86, 88-93, 96-156, 158-442

重点是每个句点可以有多个范围,但第一个和最后一个始终以" [["结束"]],"分别

尝试:

ifstream ifs("temp");
string line;
float rang1, rang2,rang3;
while(std::getline(ifs, line))
{
    istringstream iss(line);
    string Per;
    iss >> Per >> rang1 >> rang2 >> rang3;

    cout<<"For Period ====="<<Per<<" range is "<<rang1 <<"  "<< rang2 <<" "<< rang3<<endl;
}

,这给了

For Period =====251168 range is 0  0  1.32513e-37
For Period =====251244 range is 0  0  1.32513e-37
For Period =====251251 range is 0  0  1.32513e-37
For Period =====251252 range is 0  0  1.32513e-37

1 个答案:

答案 0 :(得分:2)

以下最小代码示例假定给定文件永远不会损坏。

#include <ostream>
#include <istream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <iostream>
#include <algorithm>

struct Range
{
    friend std::istream& operator>>(std::istream& is, Range& p);
    friend std::ostream& operator<<(std::ostream& os, const Range& p);

    int lower;
    int bigger;
};

std::istream& operator>>(std::istream& is, Range& p)
{
    is.ignore(1, '[');
    std::string s;
    std::getline(is, s, ']');

    auto commaPos = std::find(s.begin(), s.end(), ',');

    p.lower = std::stoi(std::string{s.begin(), commaPos});
    p.bigger = std::stoi(std::string{std::next(commaPos, 2), s.end()});

    return is;
}

std::ostream& operator<<(std::ostream& os, const Range& p)
{
    return os << "[" << p.lower << ", " << p.bigger << "]";
}

struct Period
{
    friend std::istream& operator>>(std::istream& is, Period& p);
    friend std::ostream& operator<<(std::ostream& os, const Period& p);

    std::string name;
    std::vector<Range> ranges;
};

std::istream& operator>>(std::istream& is, Period& p)
{
    std::getline(is, p.name, ':');

    is.ignore(std::numeric_limits<std::streamsize>::max(), '[');
    do
    {
        Range r;
        is >> r;
        p.ranges.push_back(r);
    } while(!(is.ignore(std::numeric_limits<std::streamsize>::max(), ' ').eof()));

    return is;
}

std::ostream& operator<<(std::ostream& os, const Period& p)
{
    os << p.name << ": [";
    for(auto a = p.ranges.begin(); a != std::prev(p.ranges.end()); ++a)
        os << *a << ", ";
    os << *(std::prev(p.ranges.end())) << "],";

    return os;
}


int main()
{
    std::vector<Period> periods;

    std::fstream inputFileStream("myinputfile", std::ios::in);
    for(std::string s; std::getline(inputFileStream, s); )
    {
        periods.push_back(Period());

        std::stringstream ss(s);
        ss >> periods.back();
    }

    for(const auto& a : periods)
    {
        std::cout << "Period : " << a.name << " with ranges ";
        for(auto b = a.ranges.begin(); b != std::prev(a.ranges.end()); ++b)
            std::cout << b->lower << "-" << b->bigger << ", ";
        auto last = std::prev(a.ranges.end());
        std::cout << last->lower << "-" << last->bigger << std::endl;
    }
}

您可以运行此online! 一个较小的痛苦方法是使用正则表达式。你可以通过visting regex101来教他们自己。要在C ++中使用正则表达式,只需查看相应的标准Regex - 库,示例应该可以帮助您。