如何将QByteArray转换为std :: istream或std :: ifstream? [英] How to convert QByteArray to std::istream or std::ifstream?
问题描述
我想在运行时从QByteArray
创建istream
,而不在QByteArray
的内存中保存物理文件.
I want to create istream
from QByteArray
at runtime, without saving a physical file in memory of QByteArray
.
我发现有很多方法可以进行相反的转换,即istream
到QByteArray
,但是没有这种转换.
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:
-
使用
QDataStream
代替std::stream
.根据文件.QDataStream::QDataStream(const QByteArray &a)
Use a
QDataStream
instead ofstd::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屋!