QByteArray读取文件跳过0x0D

派派是只大橘喵
派派是只大橘喵
发布于 2023-12-28 / 32 阅读 / 0 评论 / 0 点赞

QByteArray读取文件跳过0x0D

QByteArray读取文件跳过0x0D

今天用QFile打开一个文件,然后保存在一个QByteArray中,然后把QByteArray转换成 char* 传递给一个函数发送给下位机,发现下位机收到的数据所有0x0D都被跳过了,然后便检查了发送的数据, 后面经过排查,在打开文件的时候 QByteArray 中就已经没有 0x0D了很是奇怪 , 下面是我的代码

 //写入测试
 int a = zdev.getEeprom_size();
 int b = zdev.getData_flash_size();
 ZLog(INFO,QString::number(a)+"  "+ QString::number(b));
 QFile qf1("d:/test/dataflash.bin");
 QFile qf2("d:/test/eeprom.bin");
 bool res = qf1.open(QIODevice::ReadOnly|QIODevice::Text);
 res = res && qf2.open(QIODevice::ReadOnly|QIODevice::Text);
 if(!res){
     qDebug() << "can not open file";
 }else{
     qDebug() << "file opened";
 }
 QByteArray ba1 = qf1.readAll();
 QByteArray ba2 = qf2.readAll();

调试了几次才发现的问题,不知道为啥所以就求助万能的GPT

呜呜呜,饭碗不保了,虽然他给的代码感觉不是很对头,但是呢说到了问题的点子上,就是0x0d是回车,用 text 模式打开的话 会吃掉这个回车,下面是修改后的代码

 //写入测试
 int a = zdev.getEeprom_size();
 int b = zdev.getData_flash_size();
 ZLog(INFO,QString::number(a)+"  "+ QString::number(b));
 QFile qf1("d:/test/dataflash.bin");
 QFile qf2("d:/test/eeprom.bin");
 bool res = qf1.open(QIODevice::ReadOnly);   //删除text模式
 res = res && qf2.open(QIODevice::ReadOnly); //删除text模式
 if(!res){
     qDebug() << "can not open file";
 }else{
     qDebug() << "file opened";
 }
 QByteArray ba1 = qf1.readAll();
 QByteArray ba2 = qf2.readAll();


评论