将指针返回到第一行-QFile
Getting the pointer back to the first line - QFile
从文件中读取数据并将其保存在QHash中,如下所示:
QHash<int, QVector<float> >
我的数据文件不包含头,所以当我第一次创建向量,然后进入文件循环时,我会错过第一行的数据。我的来源是:
QFile file("...\a.csv");
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(0, "Error", file.errorString());
}
QString fileLine = file.readLine();
QStringList fileLineSplit = fileLine.split(',');
hashKeySize = fileLineSplit.size();
for(int t=0; t<hashKeySize; t++)
{
QVector<float> vec;
hash_notClustered[t] = vec;
}
while(!file.atEnd())
{
QString line = file.readLine();
QStringList list = line.split(',');
for(int t = 0; t<list.size(); t++)
{
hash_notClustered[t].push_back(list[t].toFloat());
}
}
Q: 当使用while(!file.atEnd())
循环时,如何使指针返回到第一行以避免错过第一行?
使用file.close()
关闭文件
for(int t=0; t<hashKeySize; t++)
{
QVector<float> vec;
hash_notClustered[t] = vec;
}
并且在CCD_ 3解决问题之前重新打开它。
QFile file("...\a.csv");
if(!file.open(QIODevice::ReadOnly)){
QMessageBox::warning(0, "Error", file.errorString());
}
QString fileLine = file.readLine();
QStringList fileLineSplit = fileLine.split(',');
int hashKeySize = fileLineSplit.size();
for(int t=0; t<hashKeySize; t++){
QVector<float> vec;
hash_notClustered[t] = vec;
}
do{
for(int t = 0; t<fileLineSplit.size(); t++){
hash_notClustered[t].push_back(list[t].toFloat());
}
fileLine = file.readLine();
fileLineSplit = fileLine.split(',');
}while(!fileLine.isEmpty());
C++的循环比"for"answers"while"循环多。上面的代码更有效率吗?更快?没有Bug?不知道。但至少在文件操作上更少。:-)
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 从C++dll访问C#中的一行主要参数
- 在C++中,我如何接受不同于同一行的用户输入
- 如何从一行中读取特定数量的字符?(C++)
- 读取最后一行代码算法 - c++ 时出现问题
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 有没有办法在一行中填充矢量图
- 需要将一行代码从C++ Qt翻译成PyQt
- 如何从QFile阅读?它显示该文件不可访问
- QDial:如何将 int 值传递给需要常量字符*的 Qfile?
- 为什么如果我添加这一行,我的程序会不断询问值
- 如何使代码打印文本文件中的第一行?
- 如何在C++中以这种方式返回一行文本?
- 如何使用运算符在同一行中多次调用函数
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- QDataStream 读取和写入的字节数比 QFile::length() 报告要多
- 而(!inputfile.eof())只读取第一行?
- 将指针返回到第一行-QFile