如何在没有换行符的情况下读取CSV记录"rn"?
How to read CSV record without the newline CRLF " "?
我对c++非常陌生,我一直在努力弄清楚如何将CSV文件读取为矢量。到目前为止,一切都很好,除了我不知道如何避免在每个CSV记录的末尾换行符。
下面是我的代码:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
// stream input operator overloaded to read a list of CSV fields
std::istream &operator >> (std::istream &ins, std::vector<std::string> &record)
{
record.clear();
// read the entire line into a string
std::string line;
std::getline(ins, line);
// using a stringstream to separate the fields out of the line
std::stringstream ss(line);
std::string field;
while (std::getline(ss, field, ';'))
{
// add the converted field to the end of the record
record.push_back(field);
}
return ins;
}
// stream input operator overloaded to read a list of CSV fields
std::istream &operator >> (std::istream &ins, std::vector<std::vector<std::string>> &data)
{
data.clear();
// add results from record into data
std::vector<std::string> record;
bool empty;
while (ins >> record)
{
// check if line has a price
for (unsigned i = 0; i < record.size(); i++)
{
std::stringstream ss(record[2]);
int price;
if (ss >> price)
{
empty = true;
}
else
{
empty = false;
}
}
if (empty == true)
{
data.push_back(record);
}
}
return ins;
}
int main()
{
// bidemensional vector for storing the menu
std::vector<std::vector<std::string>> data;
// reading file into data
std::ifstream infile("test.csv");
infile >> data;
// complain if theres an error
if (!infile.eof())
{
std::cout << "File does not excist." << std::endl;
return 1;
}
infile.close();
for (unsigned m = 0; m < data.size(); m++)
{
for (unsigned n = 0; n < data[m].size(); n++)
{
std::string recordQry;
recordQry += "'" + data[m][n] + "', ";
std::cout << recordQry;
}
std::cout << std::endl;
}
return 0;
}
test.csv包含:
CODE;OMSCHRIJVING; PRIJS ;EXTRA;SECTION
A1;Nasi of Bami a la China Garden; 12,00 ;ja;4
A2;Tjap Tjoy a la China Garden; 12,00 ;ja;1
A3;Tja Ka Fu voor twee personen; 22,50 ;ja;1
try:
while (std::getline(ss, field, ';'))
{
// add the converted field to the end of the record
record.push_back(field.erase(s.find('r'));
}
//record.push_back(field.erase(s.find('r'));
return ins;
我本来打算删除我的答案,但决定重新提交一个,因为不管关于getline
的所有事实都在飞,你确实知道你有这个问题。在另一个回答的评论中,我注意到你提到它最初是一个excel文件。好吧,至少在某些情况下,微软以rn
结束他们的产品线。这是原因吗?getline
仍然会放弃n
,但你仍然会有回车。如果是这种情况,您将需要使用我之前分享的一种方法。我希望我已经救赎了自己。
我检查了使用rn
写入文件,是的,它将离开r
。我在调试中观看了它,甚至当我再次使用getline
来提取它在字符串中留下的值时。当它打印到控制台时,它会将光标移到第一个字母的下方。
微软显然使用这种样式是为了向后兼容旧的机器,旧的机器需要两个单独的功能——一个是将标题返回到左边的空白,一个是卷起纸张。这听起来像你正在经历的行为吗?
相关文章:
- 正在将csv文件读取为双精度矢量
- 将两个数组中的差异记录在第三个数组中
- 如何选择在 csv 文件中输出的位置
- 禁止在控制台上记录谷神星
- 创建 Spdlog 异步文件记录器时遇到困难
- fastrtps:如何在发布/订阅级别使用 DDS 历史记录?
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 如何将自定义记录器与websocketpp一起使用?
- C++结构到德尔福记录dll调用
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 无法读取C++中的 CSV 文件
- 如何判断SSL_read是否已经接收并处理了来自单个消息的所有记录
- 流:CSV 文件中的换行符
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- 分段错误:从文件中访问csv记录时转储了核心
- 检测 CSV 文件中的记录/行分隔符
- 如何在Visual Studio 2015 c++项目调试期间将变量值更改记录到CSV文件中
- 如何在没有换行符的情况下读取CSV记录"rn"?