#include <iostream>
#include <stdlib.h>
#include <QString>
#include <QFile>
#include <qdebug.h>
#include <QTextStream>
using namespace std;
int main(void)
{
QString file_path("f:/13432420.02o");
QFile o_file(file_path);
if (!o_file.open(QIODevice::ReadOnly))
{
qDebug() << "Cannot open" << file_path << endl;
system("pause");
exit(EXIT_FAILURE);
}
QTextStream read(&o_file);
QString readString;
readString = read.readLine(); // 读第一行
qDebug() << "first line : " << readString << endl;
int index = readString.indexOf("3"); // 若所比较的字符串在该行存在,返回位置,否则为-1
// 注意,位置是从0开始
cout << "index : " << index << endl;
// mid(0,9) 从readString的第0位开始截取,截取长度为9位
// trimmed() 移除字符串两端的空白字符
QString qstrVerson = readString.mid(0,9).trimmed();
qDebug() << "QString : " << qstrVerson << endl;
double dVerson = readString.mid(0,9).toDouble();
cout << "double : " << dVerson << endl;
//int iVerson = readString.mid(0,9).toInt();
QString str = "2001";
bool ok;
int iVerson = str.toInt(&ok,10); // ok用于判断转换是否成功,10表示十进制
cout << "int : " << iVerson << endl;
cout << ok << endl;
//readString = read.readLine(); // 读第二行
//qDebug() << readString << endl;
system("pause");
return 0;
}
071-利用QT的文件读取操作
最新推荐文章于 2021-10-21 13:31:11 发布