一、打开文件
1、引头文件
#include <QFile>
2、文件打开
QFile file("path");
bool isopen = file.open(QFile::ReadOnly);
if(isopen){
qDebug() << "open txt file is success";
}
二、文件读取
1、计算File大小
qint64 length = file.size();
2、逐个字节读取
使用函数为:seek(),read()
int pos = 0; //字节位置
int read_len = 1; //每次读取字节的长度
while(pos < read_len){
file.seek(pos);
QString val = file.read(read_len);
pos++
}
三、在这里插入代码片
QFile file("C:/data.txt");
bool isopen = file.open(QFile::ReadOnly);
if(isopen){
qDebug() << "open txt file is success";
}
qint64 size = file.size();
qDebug() << size << " file size";
QByteArray data = file.readAll();
int pos = 0;
QString str;
QVector<QString> data_vac;
bool isData = false;
while (pos < size) {
file.seek(pos);
QString val = file.read(1);
if(val != " "){
isData = true;
str.append(val);
}else{
if(isData == true){
data_vac.push_back(str);
str.clear();
}
isData = false;
}
pos++;
}
PS:
1、因为数据有一定的格式,但格式又不是想要的,所以需要做一些处理;
2、要求:去除空格,换行时数据断开了要链接上;
3、实现:按字节读取,遇到空格避开,然后将有效数据存入vector里面。