在fstream c++的while循环中使用多个分隔符

Using multiple delimiters in while loop for fstream c++

本文关键字:分隔符 循环 fstream c++ while      更新时间:2023-10-16

我正在尝试用ifstream.读取.dat文件。当我读取问题时,我的问题是我需要设置2个分隔符。第一个是',',第二个是'n'

我的文件格式是这样的,第1项、第2项、第3项第4项、第5项、第6项

我的问题是用我现在的方式添加"\n"。我目前正在添加中的","作为while循环中的分隔符,如下

 while (std::getline(infile, line, ','))

但是得到"\n"会让我很沮丧。所以在while循环中,我尝试了这样的东西,

    std::getline(infile, inp.item.group, ',' );
    std::getline(infile, inp.item.total_pay, ',' );
    std::getline(infile, inp.item.quantity, 'n' );

但很明显,我不知道如何访问我的每一个项目,并给它们各自的分隔符。

我应该不能像使用新行作为分隔符那样读取整行内容,然后像在while循环中尝试那样将所有内容拆分吗?

我的相关代码如下,

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
struct Input
{
    friend std::istream& operator >>(std::istream& inp, Input& item);
    friend std::ostream& operator <<(std::ostream& outp, Input const& item);
    std::string group;
    std::string total_pay;
    float quantity;
    // default constructor. sets up zero-elements
    Input() : group(), total_pay(), quantity()
    {
    }
    Input(std::string groupIn, std::string total_payIn, float quantityIn) :
    group(std::move(groupIn)),
    total_pay(total_payIn),
    quantity(quantityIn)
    {
    }
    // Accessors
    std::string const& getGroup() const { return group; }
    std::string getTotalPay() const { return total_pay; }
    float getQuantity() const { return quantity; }
};
// global free function for extracting an Input item from an input stream
std::istream& operator >>(std::istream& inp, Input& item)
{
    return (inp >> item.group >> item.total_pay >> item.quantity);
}
// global operator for inserting to a stream
std::ostream& operator <<(std::ostream& outp, Input const& item)
{
    outp
    << item.getGroup() << ", "
    << item.getTotalPay() << ", "
    << item.getQuantity();
    return outp;
}
struct ctype : std::ctype<char>
{
    static mask* make_table()
    {
        static std::vector<mask> table(classic_table(),
                                       classic_table() + table_size);
        table[','] |= space;
        return &table[0];
    }
    ctype() : std::ctype<char>(make_table()) { }
};
int main() {
    std::ifstream infile("employee.dat");
    // one line per item enforced.
    std::vector<Input> data;
    std::string line;
    while (std::getline(infile, line))
    {
        std::istringstream iss(line);
        Input inp;
        infile.imbue(std::locale(iss.getloc(), new ctype));
        std::vector<Input> data((std::istream_iterator<Input>(std::cin)), {});
        if (iss >> inp) // calls our extraction operator >>
            data.push_back(inp);
        else
            std::cerr << "Invalid input line: " << line << 'n';
    }
    // dump all of them to stdout. calls our insertion operator <<
    std::copy(data.begin(), data.end(),
              std::ostream_iterator<Input>(std::cout,"n"));
    return 0;
}

您可以使用自定义std::ctype方面设置分隔符。然后你的格式化提取器将处理剩下的:

struct ctype : std::ctype<char>
{
    static mask* make_table()
    {
        static std::vector<mask> table(classic_table(),
                                       classic_table() + table_size);
        table[','] |= space;
        return &table[0];
    }
    ctype() : std::ctype<char>(make_table()) { }
};
// ...
int main()
{
    // ...
    infile.imbue(std::locale(iss.getloc(), new ctype));
    std::vector<Input> data((std::istream_iterator<Input>(std::cin)), {});
}