While trying to read data from .txt file using c++, I met a strange phenomenon that my program will be stucked in while loop when it tried to input char to int variable(e.g to a1).
I understand that the problem with the code below can be solved if I change int a1 = 0;
to char a1;
. My problem is why will the file pointer stops at the first character it meet? I thought it will just turn character to ASCII integer or simply skip it as if it was a tab or space.
below is the code that I come up to test this.
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream in;
int a1=0;
int a2=0,a3=0,a4=0;
in.open("input.txt");
while(!in.eof()){
in >> a1 >> a2 >> a3 >> a4;
cout << a1 << " "<< a2 << " "<< a3 << " "<< a4<<endl;
cout << SEEK_CUR <<endl;
}
return 0;
}
and following is the "input.txt"
a 42 94 32
b 33 19 82
c 88 40 18
d 92 45 19
e 77 25 84
and the output is
0 0 0 0
1
0 0 0 0
1
0 0 0 0
1
.
.
and so on