参考文章–https://blog.51cto.com/u_11531789/5046248
利用QDateStream读写二进制文件参考:https://blog.csdn.net/wzz953200463/article/details/114107102
可以使用int readRawData(char s, int len)将数据读入一个预先分配好的char;
缓冲区 s 必须被预先分配,缓冲区 s 使用 new[]分配,使用 delete 操作符销毁
可以使用int writeRawData(const char *s, int len)函数把原始数据写入datastream
使用这种方式的话,要由你自己进行所有数据的编码和解码
把 len 个字节的数据从缓冲区 s 写入流,并返回实际写入的字节数,若发生错误,则返回-1,注意:数据未编码
从流中读取最多 len 个字节到缓冲区 s 中,并返回读取的字节数,若产生错误,则返回−1,缓冲区 s 必须被预先分配。数据是未编码的。
QFile f("l.txt");QDataStream out(&f);f.open(QIODevice::WriteOnly);out< //输出全部内容,以循环的方式逐字符输出qDebug()<
QDataStream &QDataStream::writeBytes(const char *s, uint len)
先向文件写入一个quint32的数据,该值就是将要写入的数据的长度;紧接着写入相应数据
QDataStream &QDataStream::readBytes(char *&s, uint &l)
先从文件读取一个quint32值,该值就是将要读取的数据的长度,然后读取相应字节的数据到预先定义好的char*中
注意,readBytes() 不需要我们事先分配好内存
下一篇:Pinia使用