gpt4 book ai didi

c++ - 在 C++ 中将 Int 数据保存到文本文件中

转载 作者:行者123 更新时间:2023-11-28 00:31:41 24 4
gpt4 key购买 nike

我致力于将数据保存到文本文件中,并将其与另一个文本文件进行比较。以下是我处理的代码:

    ofstream outfile;
outfile.open("Data",ios::out | ios :: binary);
for(x=0; x<100; x++)
{
printf("data- %x\n", *(((int*)pImagePool)+x));
int data = *(((int*)pImagePool)+x);
//outfile<<(reinterpret_cast<int *>(data))<<endl;
outfile<<(int *)data<<endl;
}

printf 的结果是24011800从文本文件中读取的结果是 0x24011800

为什么会有0x出现了吗?我们可以删除它吗?

reinterpret_cast<int *> & (int *) 之间有什么区别?但两者都给出相同的结果?

最佳答案

因为你把它转换成一个指针,所以输出将是一个指针。

因为data是一个普通的值变量,所以照常写就可以了:

outfile << data << '\n';

我还建议您在编写 C++ 程序时停止使用 printf,没有理由使用它。而是使用 std::cout 输出:

std::cout << "data- " << *(((int*)pImagePool)+x) << '\n';

或者如果你想要十六进制输出

std::cout << "data- " << std::hex << *(((int*)pImagePool)+x) << '\n';

关于c++ - 在 C++ 中将 Int 数据保存到文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22655181/

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