一、问题描述
最近在移植qt程序到银河麒麟系统中,发现在Windows下调用seek接口更新二进制文件功能在麒麟系统中总是无法实现更新,调用的QFile接口也并未报错。
原代码实现方式如下:
if (!m_writeFile->open(QIODevice::WriteOnly | QIODevice::Append)){HLWarn() << "Write Log header err: " << m_writeFile->errorString();emit sigFileOperateErr(WriteLogFile_Type);return;}QByteArray data;QDataStream in(&data, QIODevice::WriteOnly);in << m_fileHeader;m_writeFile->seek(0);if (-1 == m_writeFile->write(data, data.size())){HLWarn() << "Write Log header err: " << m_writeFile->errorString();emit sigFileOperateErr(WriteLogFile_Type);}m_writeFile->flush();m_writeFile->close();
异常二进制文件如下:
二、解决方案
通过查阅资料及分析代码,发现与文件的打开方式有关,修改后的代码如下:
if (!m_writeFile->open(QIODevice::ReadWrite)){HLWarn() << "Write Log header err: " << m_writeFile->errorString();emit sigFileOperateErr(WriteLogFile_Type);return;}
最终文件的更新效果如下: