TITLE: catching errors with ifstream (Source: comp.lang.c++, 13 Sep 2000) JANSEN: rutgerjansen@my-deja.com > I'm reading data with an ifstream on the following way > > ifstream infile("file.txt", ios::in); > int i; > while(1){ > infile >> i; > } > > This goes ok until there is a non-integer value in the file. How do I > catch this? KUEHL: Dietmar Kuehl If you want to "catch" this, you would use something like this: infile.exceptions(std::ios_base::failbit); try { while (1) infile >> i; } catch (...) { std::cout << "error caught!\n"; } Of course, you would also catch an error if the file happens not to be infinite because attempting to read something from a stream after hitting end of file also causes a failure. Probably you just want to use code like this: while (infile >> i) ; if (!infile.eof()) std::cerr << "illegal data in the file\n"; _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com