如何将QByteArray转换为std :: istream或std :: ifstream? [英] How to convert QByteArray to std::istream or std::ifstream?

查看:756
本文介绍了如何将QByteArray转换为std :: istream或std :: ifstream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时从QByteArray创建istream,而不在QByteArray的内存中保存物理文件.

I want to create istream from QByteArray at runtime, without saving a physical file in memory of QByteArray.

我发现有很多方法可以进行相反的转换,即istreamQByteArray,但是没有这种转换.

I found that there are many ways to do the opposite conversion, i.e. istream to QByteArray, but not this one.

如何做到这一点?

推荐答案

要从QByteArray中通过std::istringstream进行阅读似乎很容易:

To read via std::istringstream from QByteArray seems quite easy:

testQByteArray-istream.cc:

#include <iostream>
#include <sstream>
#include <QtCore>

int main()
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  // make a QByteArray
  QByteArray data("Hello Qt World.");
  // convert to std::string
  std::istringstream in(data.toStdString());
  // read from istringstream
  for (;;) {
    std::string buffer;
    if (!std::getline(in, buffer)) break;
    std::cout << "Got: '" << buffer << "'\n";
  }
  // done
  return 0;
}

testQByteArray-istream.pro:

SOURCES = testQByteArray-istream.cc

QT = core

cygwin64 上进行编译和测试:

$ qmake-qt5 testQByteArray-istream.pro

$ make

$ ./testQByteArray-istream 
Qt Version: 5.9.4
Got: 'Hello Qt World.'

$

完成.停下来,等等!

不将物理文件保存在内存中

without saving a physical file in memory

我不太确定该怎么读.可能是

I'm not quite sure how to read this. Probably, it means

,不复制保存在QByteArray

without copying data saved in QByteArray

我只看到两种解决方案:

I see only two solutions:

  1. 使用QDataStream代替std::stream.根据文件. QDataStream::QDataStream(const QByteArray &a)

  1. Use a QDataStream instead of std::stream. According to doc. QDataStream::QDataStream(const QByteArray &a)

构造一个对字节数组a进行操作的只读数据流.

Constructs a read-only data stream that operates on byte array a.

听起来非常承诺不会复制数据.

This sounds very promising that data is not copied.

DIY.制作一个从std::stream派生的类,该类可以从QByteArray读取而无需复制.

DIY. Make a class derived from std::stream which may read from a QByteArray without copying.

关于2.选项,我找到了DietmarKühl对 SO:从恒定内存中创建输入流的答案.将其应用于上面的示例,它看起来像这样:

Concerning 2. option, I found Dietmar Kühl's answer to SO: Creating an input stream from constant memory. Applying this to the above sample, it would look like this:

#include <iostream>
#include <QtCore>

// borrowed from https://stackoverflow.com/a/13059195/7478597
struct membuf: std::streambuf {
  membuf(char const* base, size_t size) {
    char* p(const_cast<char*>(base));
    this->setg(p, p, p + size);
  }
};
struct imemstream: virtual membuf, std::istream {
  imemstream(char const *base, size_t size):
    membuf(base, size),
    std::istream(static_cast<std::streambuf*>(this)) {
  }
};

int main()
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  // make a QByteArray
  QByteArray data("Hello Qt World.");  
  imemstream in(data.data(), (size_t)data.size());
  // read from istringstream
  for (;;) {
    std::string buffer;
    if (!std::getline(in, buffer)) break;
    std::cout << "Got: '" << buffer << "'\n";
  }
  // done
  return 0;
}

cygwin64 上再次编译和测试:

$ qmake-qt5 testQByteArray-istream.pro

$ make

$ ./testQByteArray-istream 
Qt Version: 5.9.4
Got: 'Hello Qt World.'

$

这篇关于如何将QByteArray转换为std :: istream或std :: ifstream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆