我是一个新手,当涉及到C++,我想要做的是从.csv文件读取,并将其存储在向量中,然后显示,我的问题是代码崩溃后,最后reqd条目从终端运行时显示从文件,但在ide(codeblocks)它说sigsegv错误,当我尝试调试它...代码段错误
ps:我想要读取文件到载体的原因是能够稍后进入mysqldb
#include <vector>
#include <string>
#include <sstream>
#include<fstream>
#include <iostream>
using namespace std;
vector<string> split_at_commas(const string& row)
{
vector<string> res;
istringstream buf(row);
string s;
while (getline(buf, s, ','))
res.push_back(s);
return res;
}
int main()
{
string line;
ifstream data("Book1.csv" ,ios::out);
while(!data.eof())
{
getline(data,line,'\n');
vector<string> v = split_at_commas(line);
/*ide points error to this line*/
cout << v[0] << '\t' << v[1] <<'\t' << v[2]<< '\t'<<endl;
}
data.close();
}
你的代码缩进很糟糕,你应该感觉很糟糕。请修复 –
你可以发布你的数据文件 - 你可能有一行没有三列,所以你试图访问一个不存在的元素。 –
最有可能的是,你的最后一个记录少于3个值,这使'v [2]'违反了数组的边界。 – 2012-11-21 11:35:15