superLuke
superLuke
2018-10-25 04:02

Qt中如何将文件读出并存放在一个指针里

5
  • 类型转换
  • 文件读取
  • 指针

原程序是用CFile::Read(pionter,length)来实现的,我尝试用了QDataStream::readRawData(pointer,length)来读,但是得到的pointer为空,下面附上CFile实现的代码和我写的代码

            pOffsetBuffer = new unsigned short[dwRows*dwColumns*(sizeof(unsigned short))];

                        if (file.open(strOffsetFile,QFile::typeBinary|CFile::modeRead))
            {// 打开文件

                UINT nByte = file.Read(pOffsetBuffer, dwRows*dwColumns*sizeof(unsigned short)); // 读取文件
                file.Close();

                bOffsetOK = true;
            }
  pOffsetBuffer = new unsigned short[dwRows*dwColumns*(sizeof(unsigned short))];

    QFile rawfile(strOffsetFile);
            if (rawfile.open(QIODevice::ReadOnly))
            {// 打开文件

                QDataStream in(&rawfile);

                char* cp= new char[dwRows*dwColumns*(sizeof(unsigned short))];

                int nByte = in.readRawData(cp,dwRows*dwColumns*(sizeof(unsigned short)));

                memcpy(pOffsetBuffer,cp,dwRows*dwColumns*(sizeof(unsigned short)));
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答