通过 QDataStream 和 QTcpSocket 读取和发送文件
我的问题是变量内容始终为空。 这是我的代码:
QFile file("/home/qt/Client/file.txt");
if(file.open(QIODevice::ReadOnly)){
qDebug("File opened");
}
QDataStream fileData(&file);
QByteArray content;
QDataStream out(&content, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << static_cast<quint16>(0) << "file" << "/home/qt/Client/file.txt" << fileData;
out.device()->seek(0);
out << static_cast<quint16>(content.size());
qDebug() << content.constData();
tcpSocket->write(content);
输出:
File opened
内容始终为空
感谢您的帮助
My problem is that the variable content is always empty.
Here is my code:
QFile file("/home/qt/Client/file.txt");
if(file.open(QIODevice::ReadOnly)){
qDebug("File opened");
}
QDataStream fileData(&file);
QByteArray content;
QDataStream out(&content, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << static_cast<quint16>(0) << "file" << "/home/qt/Client/file.txt" << fileData;
out.device()->seek(0);
out << static_cast<quint16>(content.size());
qDebug() << content.constData();
tcpSocket->write(content);
Output :
File opened
content is always empty
thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
content
不为空,但如果将其解释为 C 风格的以 0 结尾的字符串,它将看起来为空。当您编写时:
这会将
content
的前两个字节设置为大端格式的content.size()
(这是默认值)。因此,如果content.size()
小于 255,则content.constData()
的第一个字节将为 0 ('\0'
)。任何使用需要 C 样式字符串的函数打印constData()
的尝试都不会输出任何内容,因为您的“字符串”以“字符串结尾”标记开头。如果你想查看
content
的完整内容,你应该单独打印它的所有字符,并使用hexdump
之类的东西来查看原始数据。如果我这样做而不是 qDebug() <<,我会得到以下结果: content.constData(); :
运行时输出(该文件仅包含 20 个
'a'
字符):如果我使用:
由于第一个 0,将不会有输出字符。
如果你的数据较长,并且
content
的大小大于255,第一个字符将不再是0,但你会打印两个垃圾字符,什么也没有否则因为 Qt 通过首先写入其长度(此处为 32 位),然后写入其内容来序列化 QString(以及大多数其他类型)。由于它采用大端表示法,因此第一个字节很有可能为 0。带注释的输出:
content
is not empty, but if you interpret it as a C-style 0-terminated string, it will appear to be empty.When you write:
This will set the first two bytes of
content
tocontent.size()
in big endian format (this is the default). So ifcontent.size()
is less than 255, the first byte ofcontent.constData()
will be 0 ('\0'
). Any attempt to printconstData()
with a function that expects a C-style string will output nothing since your "string" starts with the "end-of-string" marker.If you want to see the full contents of
content
, you should print all its chars separately and use something likehexdump
to view the raw data.Here's what I get if I do this instead of
qDebug() << content.constData();
:Output when run (the file contains just 20
'a'
chars):If I had used:
There would have been no output because of that very first 0 char.
If your data is longer, and the size of
content
is bigger than 255, the first char will no longer be 0, but you'll print two characters of garbage and nothing else because Qt serializes QString (and most other types) by first writing its length (32bit here), then its contents. Since its in big endian notation, the first byte has a very high chance of being 0.Annotated output: