reading integers from files
Posted: Sat Jun 12, 2010 4:53 pm
I'm pretty new to the whole c++ scene, and am wondering how to go about reading integers from a text file. I tried the following code, it works fine with chars but not with ints.
Code: Select all
#include <fstream>
#include <iostream>
using namespace std ;
int main()
{
int letter ;
int i ;
//string line ;
ifstream reader( "poem.txt" ) ;
if( ! reader )
{
cout << "Error opening input file" << endl ;
return -1 ;
}
else
for( i = 0; ! reader.eof() ; i++ )
{
reader.get( letter ) ;
cout << letter ;
}
reader.close() ;
cout << "Iterations: " << i << endl ;
return 0 ;
}