Qt内存映射

来源:互联网 发布:上海社保数据更新 编辑:程序博客网 时间:2024/06/06 12:30

最近在看代码的时候发现了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();}

这里写图片描述

原创粉丝点击