最近在看代码的时候发现了Qt的内存映射,原来只知道MFC有内存映射机制,可以在读取大数据文件时节约读取的时间,原来Qt中也有相应的机制,其用法更简单,下面用一个小例子演示其用法:
#include <QtCore/QCoreApplication>
#include <QFile>
#include <QDebug>
#include <iostream>
#include <stdio.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile file("1.txt");
file.open(QIODevice::ReadWrite);
qDebug() << QStringLiteral("内存大小:") << file.size();
uchar *fpr = file.map(0, file.size());
int i = 0;
int x1 = 0, x2 = 0, x3 = 0;
char *pEnd1 = NULL;
char *pEnd2 = NULL;
char *pEnd3 = (char *)fpr;
for (int i = 0 ; i < 3 ; ++i)
{
x1 = strtod(pEnd3, &pEnd1);
x2 = strtod(pEnd1, &pEnd2);
x3 = strtod(pEnd2, &pEnd3);
std::cout << x1 << "," << x2 << "," << x3 << "\n";
}
file.close();
return a.exec();
}
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓