文件读取在某些行后停止
File reading sort of stops after some lines
我必须读取一个总是有 N 行的文件 input.txt。每行由两个整数组成。具体来说,我正在读取一个文件,其中两个整数都是 2^(line_index -1)。
int temp1, temp2;
std::vector<int> vec1, vec2;
std::fstream fh("input.txt", std::ios_base::in);
for (int i = 0; i < N; i++) {
fh >> temp1 >> temp2;
vec1.push_back(temp1);
vec2.push_back(temp2);
}
//first few lines of input are
//1 1
//2 2
//4 4
// . . .
//Line 31 should be: 2147483647 2147483647
//but my code read it as 2147483647 1073741824
//This is always the case for N>30
在第 30 行之后,如您在上面的代码片段中所见,文件读取变得很奇怪。我的代码有问题吗?还是我读取文件的方法限制了我可以输入的变量?
您已达到整数限制,请参见 this question 示例。不确定你为什么要这样做,但如果你想保存更大的值,你需要不同的数据类型。
我必须读取一个总是有 N 行的文件 input.txt。每行由两个整数组成。具体来说,我正在读取一个文件,其中两个整数都是 2^(line_index -1)。
int temp1, temp2;
std::vector<int> vec1, vec2;
std::fstream fh("input.txt", std::ios_base::in);
for (int i = 0; i < N; i++) {
fh >> temp1 >> temp2;
vec1.push_back(temp1);
vec2.push_back(temp2);
}
//first few lines of input are
//1 1
//2 2
//4 4
// . . .
//Line 31 should be: 2147483647 2147483647
//but my code read it as 2147483647 1073741824
//This is always the case for N>30
在第 30 行之后,如您在上面的代码片段中所见,文件读取变得很奇怪。我的代码有问题吗?还是我读取文件的方法限制了我可以输入的变量?
您已达到整数限制,请参见 this question 示例。不确定你为什么要这样做,但如果你想保存更大的值,你需要不同的数据类型。