0

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
user45277
  • 21
  • 3
  • 1
    "*My problem is why will the file pointer stops at the first character it meet?*" - because that was the design decision. "*I thought it will just turn character to ASCII integer or simply skip it as if it was a tab or space*" - it certainly could, but peoplel in charge chose otherwise. The behaviour is as it is and asking **why** is it like so is, in such case, quite pointless, unless asked to the ones who made that decision. We may now only speculate - it may appear that this is the most intuitive approach. – Fureeish Nov 18 '19 at 13:10
  • You want to read an int, but encounter 'a', which is not an int, so the reading stops. That's the whole magic... – U. W. Nov 18 '19 at 13:16
  • "I thought it will just turn character to ASCII integer or simply skip it". So it turns out not to be the case. What now? Do you have Plan B? – n. 'pronouns' m. Nov 18 '19 at 13:24
  • @n.'pronouns' yeah just change the type of a1 to char. – user45277 Nov 18 '19 at 13:29
  • @Fureeish thx I guess it's the most reasonable explanation. – user45277 Nov 18 '19 at 13:34
  • About [`while(!in.eof())`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – AProgrammer Nov 18 '19 at 14:05
  • @AProgrammer what do you mean? – user45277 Nov 20 '19 at 16:11
  • You are using `eof()` in the control condition of a loop, I'm pointing to a question which shows that's usually not a good idea. – AProgrammer Nov 20 '19 at 16:19

1 Answers1

0

The operations in istream are type-safe and type-sensitive.

operator>>:

If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set. (until C++11)

If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits::max() or std::numeric_limits::min() is written and failbit flag is set. (since C++11)

Basically, you fail to read any data - so, you cannot reach the end of the file - that's why the infinite loop.

Community
  • 1
  • 1
David Nagy
  • 430
  • 1
  • 2
  • 7