473,385 Members | 1,814 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,385 software developers and data experts.

weird iostream/fstream behavior

So I'm having some weird problems with file output. If I try to boil
this problem down to a small, simple program to just show what problems
I'm having, I can't get the same problematic behavior. I have a
function:

bool Tourist::CreateMaps() {
debug.Out() << "Tourist::CreateMaps" << std::endl;
std::ofstream out((player_name + ".sav").c_str(),std::ios::out);

std::string ms = Random::MapStart();
std::string ma = Random::RandomMapArena();
std::string mr = Random::RandomMapRooms(3);
std::string me = Random::MapEnd();

debug.Out() << "mr is " << mr << std::endl;

out << "4\n";
out << ms << std::flush;
out << ma << std::flush;
out << mr << std::flush;
out << me << std::flush;

out.close();

return true;
}

debug.Out returns an ostream & that outputs to a debuggin file. In the
debugging file I see the output of "mr is" and it looks correct.
Specifically, there is a line "3 43 1 33 1 1 0". (For this example,
player_name = "jim".) However, in jim.sav the line "3 33 -1075888520 33
-1075888520 1 0" is written. If I change the std::flush's to
std::endl's, the correct line shows up in jim.sav (but the extra endl's
break the file and it can't be read in correctly.)

Any idea what the heck is going on here? I can't figure out how or why
the line is getting mangled. Any more information I need to provide? The
member function Random::RandomMapRooms returns a string that is build
using a string stream and converted using the str() member.
--
Jim Strathmeyer
Dec 26 '05 #1
5 2356
Jim Strathmeyer wrote:
So I'm having some weird problems with file output. If I try to boil
this problem down to a small, simple program to just show what problems
I'm having, I can't get the same problematic behavior. I have a
function:

bool Tourist::CreateMaps() {
debug.Out() << "Tourist::CreateMaps" << std::endl;
std::ofstream out((player_name + ".sav").c_str(),std::ios::out);

std::string ms = Random::MapStart();
std::string ma = Random::RandomMapArena();
std::string mr = Random::RandomMapRooms(3);
std::string me = Random::MapEnd();

debug.Out() << "mr is " << mr << std::endl;

out << "4\n";
out << ms << std::flush;
out << ma << std::flush;
out << mr << std::flush;
out << me << std::flush;
You don't need to do that. close() flushes the buffer automatically.
out.close();

return true;
}

debug.Out returns an ostream & that outputs to a debuggin file. In the
debugging file I see the output of "mr is" and it looks correct.
Specifically, there is a line "3 43 1 33 1 1 0". (For this example,
player_name = "jim".) However, in jim.sav the line "3 33 -1075888520 33
-1075888520 1 0" is written.
What are these values? Where do they come from? The large negative
numbers usually mean you are accessing invalid memory, such as already
deleted objects.
If I change the std::flush's to
std::endl's, the correct line shows up in jim.sav (but the extra endl's
break the file and it can't be read in correctly.)

Any idea what the heck is going on here? I can't figure out how or why
the line is getting mangled. Any more information I need to provide? The
member function Random::RandomMapRooms returns a string that is build
using a string stream and converted using the str() member.


Are you returning an object or a pointer to a buffer? For example, are
you returning

oss.str().c_str()

? If so, the stream is already destroyed and the buffer does not exist
anymore. It looks like the problem is before the output, not the output
itself. Try to output the same value in two different streams at the
same place:

jim_sav << the_string;
jim_sav.close();

std::cout << the_string << std::flush;

If the first one is broken but the second one works, the file stream is
probably corrupted.
Jonathan

Dec 26 '05 #2
Jonathan Mcdougall <jo***************@gmail.com> schrieb:
debug.Out returns an ostream & that outputs to a debuggin file. In
the debugging file I see the output of "mr is" and it looks correct.
Specifically, there is a line "3 43 1 33 1 1 0". (For this example,
player_name = "jim".) However, in jim.sav the line "3 33 -1075888520
33 -1075888520 1 0" is written.
What are these values? Where do they come from? The large negative
numbers usually mean you are accessing invalid memory, such as already
deleted objects.
Yeah, guess I didn't really explain it. The exact line should be "3 43 1
33 1 1 0". This is the line of text in the string (the lines being
separated by \n's.)
Are you returning an object or a pointer to a buffer? For example, are
you returning oss.str().c_str()


I'm returning oss.str(), which I think returns a string object which
gets correctly copied when it is returned. (I understand why the buffer
would no longer exist and should not be returned.)

--
Jim Strathmeyer
Dec 26 '05 #3

"Jim Strathmeyer" <st*********************************@ipass.net> wrote in
message news:Qc******************************@adelphia.com ...
So I'm having some weird problems with file output. If I try to boil
this problem down to a small, simple program to just show what problems
I'm having, I can't get the same problematic behavior. I have a
function:

bool Tourist::CreateMaps() {
debug.Out() << "Tourist::CreateMaps" << std::endl;
std::ofstream out((player_name + ".sav").c_str(),std::ios::out);

std::string ms = Random::MapStart();
std::string ma = Random::RandomMapArena();
std::string mr = Random::RandomMapRooms(3);
std::string me = Random::MapEnd();

debug.Out() << "mr is " << mr << std::endl;

out << "4\n";
out << ms << std::flush;
out << ma << std::flush;
out << mr << std::flush;
out << me << std::flush;

out.close();

return true;
}

debug.Out returns an ostream & that outputs to a debuggin file. In the
debugging file I see the output of "mr is" and it looks correct.
Specifically, there is a line "3 43 1 33 1 1 0". (For this example,
player_name = "jim".) However, in jim.sav the line "3 33 -1075888520 33
-1075888520 1 0" is written. If I change the std::flush's to
std::endl's, the correct line shows up in jim.sav (but the extra endl's
break the file and it can't be read in correctly.)

Any idea what the heck is going on here? I can't figure out how or why
the line is getting mangled. Any more information I need to provide? The
member function Random::RandomMapRooms returns a string that is build
using a string stream and converted using the str() member.


What happens when you don't do the flush? How happens if you make it:

out << "4\n" << ms << ma << mr << me << std::endl;
out.close();
Dec 27 '05 #4
Jim Langston <ta*******@rocketmail.com> schrieb:
What happens when you don't do the flush? How happens if you make it: out << "4\n" << ms << ma << mr << me << std::endl;
out.close();


The same thing that happens with the flushes; just showing that the
flushes don't solve anything, but replacing them with std::endl's does,
which is weird, because all the endl does is output a '\n' and do a
flush.

--
Jim Strathmeyer
Dec 27 '05 #5

Jim Strathmeyer wrote:
Jonathan Mcdougall <jo***************@gmail.com> schrieb:
debug.Out returns an ostream & that outputs to a debuggin file. In
the debugging file I see the output of "mr is" and it looks correct.
Specifically, there is a line "3 43 1 33 1 1 0". (For this example,
player_name = "jim".) However, in jim.sav the line "3 33 -1075888520
33 -1075888520 1 0" is written.

What are these values? Where do they come from? The large negative
numbers usually mean you are accessing invalid memory, such as already
deleted objects.


Yeah, guess I didn't really explain it. The exact line should be "3 43 1
33 1 1 0". This is the line of text in the string (the lines being
separated by \n's.)
Are you returning an object or a pointer to a buffer? For example, are
you returning

oss.str().c_str()


I'm returning oss.str(), which I think returns a string object which
gets correctly copied when it is returned. (I understand why the buffer
would no longer exist and should not be returned.)


The problem is not with the string nor with the output. It is with the
values which are being concatenated in the string. It looks like third
and fifth objects generating the values are invalid while the others
are (seemingly) ok, though you never know. This looks a lot like
undefined behavior.

Would it be possible to show a bit more code? Showing how the values
are obtained would help.
Jonathan

Dec 27 '05 #6

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

Similar topics

1
by: gukn9700 | last post by:
When I used fstream to handle a file, I met with a very weird thing: #include <fstream> #include <iostream> using namespace std; int main() { fstream outfile;
10
by: John Tiger | last post by:
Can anybody have idea about the difference between #include <iostream.h> and #include <iostream>. Is later one valid statement on any compiler. I tried compiling on MSVC second statement give...
11
by: Charles L | last post by:
I have read that the inclusion of <fstream.h> makes the inclusion of <iostream.h> unnecessary. Is this correct? Charles L
2
by: NewToCPP | last post by:
I am having some trouble including "iostream" "fstream" to my code. I tried the following ways and base times it did not work. What is the problem? test.cc: ======= ..... #include...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
8
by: nickyeng | last post by:
I have written 3 files, i dont know whether i do it correctly or wrongly but somehow it compiled well and can run. My simple aim is to display the terrain.txt file into the terrain array, and then...
5
by: eagerlearner | last post by:
When I open .html file i got a few weird characters printed out which preceding the content inside the .html file. But when I have the same file content with .txt extension, it has not problem....
9
by: Active8 | last post by:
Hi: Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in a console program. I use cin.getline() and sometimes just cin Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?)...
2
by: TamaThps | last post by:
Hi, I'm using visual studio 2008 and normally when I get an error it shows what line it is on and which file etc. The error I'm getting I don't know how to solve or even what the problem is. This...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.