質問

QTemporaryFile tf;
tf.open ();
QDataStream tfbs (&tf);
tfbs << "hello\r\n" << "world!\r\n";
const int pos = int (tf.pos ());

QByteArray ba;
ba.append ("hello\r\n");
ba.append ("world!\r\n");
const int size = ba.size ();

Basically my question is, what am I doing wrong? Why is pos > size? Should I not be using << ? Should I not be using QDataStream?

Edit: Is there a way to configure QDataStream or QTemporaryFile so that the << operator doesn't prepend strings with 32bit lengths and store the null terminators in the file? Calling QDataStream::writeBytes when I just have a series of quoted strings and QStrings makes for very ugly code.

役に立ちましたか?

解決

The answer is in the docs. I'm not going to go over QByteArray, as I believe it's fairly obvious that it is working as expected.

The QDataStream operator<<(char*) overload evaluates to the writeBytes() function.

This function outputs:

Writes the length specifier len and the buffer s to the stream and returns a reference to the stream. The len is serialized as a quint32, followed by len bytes from s. Note that the data is not encoded.

So for "hello\r\n", I would expect the output to be:

0,0,0,8,'h','e','l','l','o','\r','\n',0

The 4-byte length, followed by the bytes from the string. The string-ending NULL is probably also being added to the end, which would account for the otherwise mysterious extra two bytes.

他のヒント

So I ended up writing my own helper class to serialize my data:

class QBinaryStream
{
public:
    QBinaryStream (QIODevice& iod) : m_iod (iod) {}
    QBinaryStream& operator << (const char* data)
    {
        m_iod.write (data);
        return *this;
    }
    QBinaryStream& operator << (const QString& data)
    {
        return operator << (data.toUtf8 ());
    }
    QBinaryStream& operator << (const QByteArray& data)
    {
        m_iod.write (data);
        return *this;
    }

private:
    QIODevice& m_iod;
};

Should I not be using QDataStream?

In your case maybe QTextStream or even QString would do.

The QTextStream class provides a convenient interface for reading and writing text.

QTextStream can operate on a QIODevice, a QByteArray or a QString. Using QTextStream's streaming operators, you can conveniently read and write words, lines and numbers.

As for QByteArray, Qstring should be preferred to it whenever possible

The QByteArray class provides an array of bytes.

QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.

In addition to QByteArray, Qt also provides the QString class to store string data. For most purposes, QString is the class you want to use. It stores 16-bit Unicode characters, making it easy to store non-ASCII/non-Latin-1 characters in your application. Furthermore, QString is used throughout in the Qt API. The two main cases where QByteArray is appropriate are when you need to store raw binary data, and when memory conservation is critical (e.g., with Qt for Embedded Linux).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top