Qt4.6,QDataStream类的参考文档中写道:
Example (write binary data to a stream):
- QFile file("file.dat");
- file.open(QIODevice::WriteOnly);
- QDataStream out(&file); // we will serialize the data into the file
- out << "the answer is"; // serialize a string
- out << (qint32)42; // serialize an integer
Example (read binary data from a stream):
- QFile file("file.dat");
- file.open(QIODevice::ReadOnly);
- QDataStream in(&file); // read the data serialized from the file
- QString str;
- qint32 a;
- in >> str >> a; // extract "the answer is" and 42
我原样搬进了程序中测试一下,结果你猜怎么着?
42被读出来了,但原先写入的字符串"the answer is" 读出以后变成了乱码:
把程序中写入部分修改一下:
- out << (QString)"the answer is"; // serialize a string
就OK了。
不知道为什么示例代码不对,请高人指点一下。
[ 此帖被xxf_cz在2009-12-16 21:31重新编辑 ]