在fstream c++的while循环中使用多个分隔符
Using multiple delimiters in while loop for fstream c++
我正在尝试用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)), {});
}
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 使用分隔符结束用户输入的循环
- 在fstream c++的while循环中使用多个分隔符
- 如何在C++中声明两个相互包含(循环依赖)的类,并使用分隔符