#include<iostream>
#include<fstream>
#include<string>
#include<vector>
usingnamespacestd;
voidcsvline_populate(vector<string> &record, const string& line, chardelimiter);
intmain(int argc, char *argv[])
{
vector<string> row;
string line;
ifstream in("input.csv");
if (in.fail()) {cout <<"File not found" <<endl;return0; }
while(getline(in, line) &&in.good())
{
csvline_populate(row, line, ',');
for(int i=0,leng=row.size();i<leng;i++)
cout <<row[i] <<"\t";
cout<<endl;
}
in.close();
return 0;
}
voidcsvline_populate(vector<string> &record, const string& line, chardelimiter)
{
int linepos=0;
int inquotes=false;
char c;
int i;
int linemax=line.length();
string curstring;
record.clear();
while(line[linepos]!=0&& linepos<linemax)
{
c =line[linepos];
if (!inquotes &&curstring.length()==0&&c=='"')
{
//beginquotechar
inquotes=true;
}
else if (inquotes &&c=='"')
{
//quotechar
if( (linepos+1 <linemax) &&(line[linepos+1]=='"'))
{
//encountered 2 double quotesin a row (resolves to 1 double quote)
curstring.push_back(c);
linepos++;
}
else
{
//endquotechar
inquotes=false;
}
}
else if (!inquotes &&c==delimiter)
{
//end offield
record.push_back( curstring );
curstring="";
}
else if (!inquotes &&(c=='\r'|| c=='\n'))
{
record.push_back( curstring );
return;
}
else
{
curstring.push_back(c);
}
linepos++;
}
record.push_back( curstring );
return;
}
参考博客点击打开链接
//qt逐行读取文件
QFile file("/proc/modules");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString line = in.readLine();
while (!line.isNull()) {
process_line(line);
line = in.readLine();
}
参考博客
点击打开链接