Re: File reading problem
On Jul 15, 8:12 pm, "BobR" <removeBadB...@worldnet.att.net> wrote:
[...]
void readit( std::vector<s_t> &vec ){ // non-const
ifstream in("foo.txt");
if( not in.is_open() ){ std::cerr<<"ERROR"; return; }
int x( 0 ), y( 0 );
in >> x >> y;
You probably want to check the return values here. But I'm not
too sure what this function is designed to do, to begin with.
if( in.peek() == '\n' ){ in.ignore(); }
What's the purpose of this line? The following "in >> temp"
will automatically skip any leading white space (and '\n' is
white space).
If the input is line oriented, and you want to verify its
format, then you should probably read using getline, then use
istringstream to parse it (not forgetting a final
if ( line >> std::ws && line.get() == EOF ) ...
to ensure that there's no trailing garbage). If it's just a
collection of white space separated s_t, then you can just read,
you don't need to "ignore" anything.
s_t temp;
while( in >> temp ) vec.push_back( temp );
// untested. replace the above two lines with:
// for( s_t temp; in >> temp; /*m_t*/ ){
Doesn't work. The first pass through the loop will use an
uninitialized temp.
// vec.push_back( temp );
// } // for(in)
} // readit( std::vector<s_t>&)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34