On Mar 1, 9:14 am, Gary Wessle <phd...@yahoo.comwrote:
please look at the code below, the problem I have is that line 37 does
not print out into the file as expected. however if I move it to be
immediately after line 16 it prints fine into the file. apparently the
while block is doing something wicked and corrupting the stream.
how can I fix this?
11 std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
20 while( !found_it && getline( iofs, line ) )
21 {
....
32 }
33 // value does not exist, create it with a default
34 if( lookup == "reversals" )
35 {
36 val = 0;
37 iofs << "reversals" << " " << val << std::endl;
38 m_pair_status[lookup] = val;
39 } // add other lookups as needed
You're only writing this if the lookup failed, in which case getline
will have hit EOF and set various stream state flags. Consider
clearing some before trying to write. Details should be in
Stroustrup's The C++ Programming Language and various online
resources.