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 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
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
"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();
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
|
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...
|
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
|
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...
|
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...
|
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...
|
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....
|
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?)...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
| |