Qt语言如何读取CSV文件

来源:互联网 发布:李善姬在韩国地位知乎 编辑:程序博客网 时间:2024/05/08 12:49
/*************存储数据**************/
QList<QString> stringList;
QVector<QVector<QString>> dataVector;
QString str;
int const rowCount = 361;
int const columnCount = 949;
//double* dataArray = new double[rowCount*columnCount];
QFile file("china_sites_20140513.csv");
if (file.open(QIODevice::ReadOnly))
{
while (!file.atEnd())
{
qint64 lineLen; //用于按行读取数据
char buffer[5800];//存储每行的字符串
lineLen = file.readLine(buffer, sizeof(buffer));
if (lineLen != -1)//如果读取成功
{
str = buffer;
stringList = str.split("\r", QString::SkipEmptyParts);//按换行分割
str = stringList[0];
stringList = str.split("\n", QString::SkipEmptyParts);//按回车键分割
str = stringList[0];
stringList = str.split(",", QString::SkipEmptyParts);//按逗号分割
QListIterator<QString> li(stringList);
int i = 0;
QVector<QString> temp_dataList(i, 0);
for (; li.hasNext();)
{
//double temp;
QString temp;
//temp = li.next().toDouble();//把字符串转换成double型数据
temp = li.next();
temp_dataList.push_back(temp);
i++;
}
dataVector.push_back(temp_dataList);
}
}
}
//测试:
for (int i = 0; i < dataVector.size(); i++)//dataVector.size()=向量的行数
{
for (int j = 0; j < columnCount; j++)
{
if (j != columnCount - 1)
{
QByteArray ba=dataVector[i][j].toLocal8Bit();//toLocal8Bit()返回一个QByteArray类型  
cout << ba.data();//data()返回char *  
//其它类型变量还是可以照常输出的。  
printf(",");
}
else
{
QByteArray ba = dataVector[i][j].toLocal8Bit();//toAscii()返回一个QByteArray类型  
std::cout << ba.data();//data()返回char *  
printf("%\n");
}
}
//cout << endl;
}
0 0
原创粉丝点击