在抛出std :: out_of_range;错误?
我尝试阅读一些CSV数据,但是我一直遇到此错误:
丢弃'std :: out_of_range'
的实例终止 what():vector :: _ m_range_check:__n(是1)> = this-> size() (是1)
customer.csv
id, name, address
1, "Knut", "Knutveien 3"
2, "Lise", "Liseveien 7"
main.cpp
#include <iostream>
#include "rapidcsv/rapidcsv.h"
using namespace std;
void read_customer()
{
rapidcsv::SeparatorParams sp; // make a object sp
sp.mTrim = true; // remove the line space, equal to true
// open the customer document
rapidcsv::Document doc_customer("customers.csv", rapidcsv::LabelParams(), sp);
for (int i = 0; i < doc_customer.GetRowCount(); i++)
{
auto name = doc_customer.GetCell<string>("name", i);
auto address = doc_customer.GetCell<string>("address", i);
cout << "customer: " << name << ", " << address << endl;
}
}
int main()
{
read_customer();
return 0;
}
I try to read some csv data, but I keep getting this error:
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 1) >= this->size()
(which is 1)
customer.csv
id, name, address
1, "Knut", "Knutveien 3"
2, "Lise", "Liseveien 7"
main.cpp
#include <iostream>
#include "rapidcsv/rapidcsv.h"
using namespace std;
void read_customer()
{
rapidcsv::SeparatorParams sp; // make a object sp
sp.mTrim = true; // remove the line space, equal to true
// open the customer document
rapidcsv::Document doc_customer("customers.csv", rapidcsv::LabelParams(), sp);
for (int i = 0; i < doc_customer.GetRowCount(); i++)
{
auto name = doc_customer.GetCell<string>("name", i);
auto address = doc_customer.GetCell<string>("address", i);
cout << "customer: " << name << ", " << address << endl;
}
}
int main()
{
read_customer();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输入数据看起来不像常规CSV数据。如果您从数据中删除空白行,然后将其更改为类似的内容:
在尝试阅读它之前,
appapcsv
能够正确解析它。除了使用不同的文件名
customers.csv
外,C ++代码看起来还不错。为了完整性,我从
customer.csv
中删除了空白行,并使用了以下main.cpp
contents可靠地解析文件。The input data does not look like regular CSV data. If you remove the blank lines from the data, and change it into to something like:
before trying to read it,
rapidcsv
is able to parse it correctly.The C++ code looks OK aside from using different filename
customers.csv
than specified in the question (customer.csv
).For completeness, I removed the blank lines from
customer.csv
and used the followingmain.cpp
content which succefully parsed the file.