gpt4 book ai didi

c++ - Qt C++ QDataStream读取数字13

转载 作者:行者123 更新时间:2023-11-28 07:39:58 25 4
gpt4 key购买 nike

我想用 QDataStream 写一个二进制文件。问题出在下面的代码中,当我写 my_string = "13"时,我读到 0 ;当 my_string 不等于“13”(“12”、“14”、“20”、...)时,我读取了实际值(12、14、20、...)。有谁知道为什么我只对数字 13 有这个问题。

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

//open the file to write
QFile file1("test");
if (!file1.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QDataStream stream1(&file1);
QString my_string = "13";

qint8 val8 = my_string.toInt();
stream1 << val8;

file1.close();


//open the file to read
QFile file2("test");
file2.open(QIODevice::ReadOnly | QIODevice::Text);

QDataStream stream2(&file2);

qint8 val2;
stream2>> val2;

QListWidgetItem *item = new QListWidgetItem;
item->setText(QString::number(val2));
ui->listWidget->addItem(item);

file2.close();
}

最佳答案

您以文本模式打开文件并写入二进制值。代码为 13 (0xD) 的字符是换行符。通过删除 QIODevice::Text 以二进制模式打开文件,它应该开始工作。

关于c++ - Qt C++ QDataStream读取数字13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16038843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com