每次我通过ifstream读取一个特定的输入时,输入就会改变,可能是int溢出
Everytime I read in through ifstream for a specific input, the input changes, possible int overflow
每次我使用ifstream作为程序的大输入时,我都会得到一些奇怪的东西。我有一种感觉,这与整数溢出有关,但我的程序仍然不能处理unsigned long long。下面是我的代码的简化版本,仍然显示错误:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream fout ("namenum.out");
ifstream fin ("namenum.in");
unsigned long long serial;
fin >> serial;
ifstream myReadFile;
cout << serial << endl;
return 0;
}
下面是奇怪的输入(或较大的输入):
5747867437
下面是我从cout得到的输出:
1452900141
我不知道是什么原因造成的。
这是我以前几乎没有给过的建议:总是检查你的输入在你试图阅读之后(感觉,我只给过这个建议几千次,所以很容易错过)。流无法预测你要读的内容,所以要确保它能正常工作:
if (fin >> searial) {
fout << serial << 'n';
}
else {
std::cerr << "failed to read the valuen";
}
查看您的代码,我大约100%肯定文件未打开(即流在尝试读取之前处于坏状态)或声称的内容不在文件中。
首先,您提供的数字肯定在unsigned long long的限制之内。其次,在unsigned long long中使用>>需要c++ 11的支持。c++ 0x支持unsigned long.
我复制了你的代码并创建了一个名为namenum的文件。在namenum中输入"5747867437"。使用UTF8编码那么输出恰好是5747867437
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 变量没有改变?通过向量的函数调用
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 当我使用regasm时,所有参数都被键入为输入?这可以改变吗
- c++在WinAPI中根据用户输入改变图像
- 每次我通过ifstream读取一个特定的输入时,输入就会改变,可能是int溢出
- Cuda内核不改变输入数组
- 在不同的处理器中改变输入参数