gpt4 book ai didi

c++ - 在 fstream c++ 的 while 循环中使用多个定界符

转载 作者:行者123 更新时间:2023-11-28 06:37:50 25 4
gpt4 key购买 nike

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

我的文件格式是这样的,项目 1、项目 2、项目 3item4,item5,item6

我的问题是用我现在的方式添加'\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 facet 设置分隔符。然后您的格式化提取器将处理其余部分:

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)), {});
}

关于c++ - 在 fstream c++ 的 while 循环中使用多个定界符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26513357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com