473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

while getline corrupting the stream


Hi

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?

thank you
1 #include <fstream>
2 #include <iostream>
3 #include <string>
4
5 /* Reads the value for the lookup string from file, and sets the value in m_pair_status
6 if the value does not exist, create it with a default.
7 */
8 double Grid::status( std::string lookup)
9 {
10 std::string f = "my_file_name";
11 std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
12 if (!iofs.is_open())
13 {
14 iofs.open( f.c_str(), stdios_base::in | ios_base::out |
15 ios_base::trunc );
16 }
17 std::string line, word;
18 double val;
19 bool found_it = false;
20 while( !found_it && getline( iofs, line ) )
21 {
22 std::stringstream ss( line );
23 ss >word;
24 if( word == lookup )
25 {
26 found_it = true;
27 ss >val;
28 m_pair_status[lookup] = val;
29 iofs.close();
30 return val;
31 }
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
40 iofs.close();
41 return val;
42 }
Mar 1 '07 #1
4 1828
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?
This looks fairly wicked to me:
29 iofs.close();
37 iofs << "reversals" << " " << val << std::endl;
Michael

Mar 1 '07 #2
"Michael" <mc******@aol.comwrites:
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?

This looks fairly wicked to me:
29 iofs.close();
37 iofs << "reversals" << " " << val << std::endl;
well, the return statement in line 30 prevents opening a closed stream
if thats what you mean.
Mar 1 '07 #3
well, the return statement in line 30 prevents opening a closed stream
if thats what you mean.
My bad. Shouldn't try answering these late in the day.

Mar 1 '07 #4
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.

Mar 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: muser | last post by:
I have a logical error in my program, I have submitted the program and my tutor hasn't listed the other problems with the code, but said that the program won't run because of a while statement....
1
by: Chris Coleman | last post by:
Hi, As some background, I have implemented a new stream object to work with tcp socket streams. I derive a new socket stream from basic_streambuf. I then derive a my sream object from my...
5
by: Aleander | last post by:
First of all, greatings from Italy to everyone, and sorry for my bad english!! I have this problem with the instruction cin.getline that follow a simple cin instruction: Example: #include...
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.