___2017

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
受不了xxxx恶心人的行为,遂搬迁至博客园。
始发:2014-08-22 17:46:24

QIODevice::Text参数有下示说明:

QIODevice::Text
0x0010
使用该选项后:
对于读:end-of-line结束符被转译为'\n';
对于写:end-of-line结束符被转译本地编码方式对应的字符。比如,在Win32下,是'\r\n'

 

 

 

在使用QIODevice::Text选项读写二进制文件时,由于经过上述转译,造成读写数据不一致,测试示例如下:

if (!fileRead.open(QIODevice::ReadOnly | QIODevice::Text)) {
    qDebug() << "file open error!\r\n";
    //return (0);
}
if (!fileWrite.open(QIODevice::WriteOnly | QIODevice::Text)) {
    qDebug() << "file not exist!\r\n";
}

在Unix类系统中,结束符为“\n(0x0A)”;Win系统中,结束符为“\r\n(0x0D 0x0A)”。所以原bin的0A被隐含转化为了0D 0A。

 

去掉QIODevice::Text选项后,读写的数据是一致的(测试数据量8k多字节):

if (!fileRead.open(QIODevice::ReadOnly)) {
	qDebug() << "file open error!\r\n";
	//return (0);
}
if (!fileWrite.open(QIODevice::WriteOnly)) {
	qDebug() << "file not exist!\r\n";
}

 

posted on 2020-06-16 10:43  yin'xiang  阅读(658)  评论(0编辑  收藏  举报