1

A very simple program: read a file line by line (each line contains integer) then do something and write the output to a file.

int main()
{
  ifstream fin ("f:\in.txt");
  ofstream fout ("f:\out.txt");

  int a;
  while (fin >> a) {
      int b = (a >> 6) & 255;
      fout << b << endl;
  }
  return 0;
}  

The input as multiple lines like this:

93859312
2635577168
2929619024
312396812
3019231016
3139200356
...

But the while loops is iterated only one time!! and output only contains

183

Which corresponds to the first input line. Why???

4

1 回答 1

8

The numbers after the first one are larger than an int can represent.

Instead of int a;, use long long int a;

The largest value than an int can represent is 2,147,483,647: What is the maximum value for an int32?

Your first value is less than this, but your second is not. Thus (fin >> a) fails (i.e. is not true), and your program exits from the while loop.

于 2013-03-30T08:38:27.180 回答