ofstream problems

F

Felix85

53 void room2File(room r){
54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");
55
56 outfile << r.getRoomNumber() << "\n";
57 outfile << r.getRoomName() << "\n";
58 outfile << r.getRoomDescription() << "\n";
59 int i;
60 for(i = 0; i > sizeof(r.getRoomExits()); i++){
61 if(i = 5){
62 outfile << r.getRoomExits();
63 } else { outfile << r.getRoomExits() << "
"; }
64 }
65 }

i get the error:
room.cpp:54: error: invalid operands of types `const char*' and `const
char[6]' to binary `operator+'

im using gcc version 3.4.5 (Gentoo 3.4.5-r1, ssp-3.4.5-1.0, pie-8.7.9)

so far I can't see anything wrong with it, so any help will be
appreciated.
 
T

Tilman Kuepper

Hi Felix,
54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");

#include <sstream>

std::eek:stringstream filename;
filename << "../gamefiles/rooms/"
<< r.getRoomNumber() << ".room";
std::eek:fstream outfile(filename.str().c_str());

Best regards,
Tilman
 
J

Jim Langston

Felix85 said:
54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");

i get the error:
room.cpp:54: error: invalid operands of types `const char*' and `const
char[6]' to binary `operator+'

im using gcc version 3.4.5 (Gentoo 3.4.5-r1, ssp-3.4.5-1.0, pie-8.7.9)

so far I can't see anything wrong with it, so any help will be
appreciated.
You are trying to add a character array ".../gamefiles/rooms/" to whatever
r.getRoomNumber() returns (proably an integer) to another character array
".room"

You can't add them together this way, because character arrays are stored as
pointers.

If you didn't have r.getRoomNumber() in there it would be a simple matter to
use std::string to do it.

ofstream outfile( std::string("../gamefiles/rooms/") + ".room" );
or such, because you can add two std::strings to gether, and you can add a
char array to a std::string. You still can't add an integer to it.

Still using std::string you can use some conversion to convert the integer
to a std::string. I use a template I call StrmConvert() which looks like
this:

#include <sstream>
template<typename T, typename F > T StrmConvert( F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}

and is used like this:
StrmConvert<std::string>( SomeNumber );

So then you could do:

ofstream outfile( ("../gamefiles/rooms/" + StrmConvert<std::string>(
getRoomNumber() ) + ".room").c_str());

This uses a few "tricks", one being if of the arguments to add is a
std::string, then they'll add together to form a std::string. So it adds
the char array to the std::string produced by the StrmConvert and gets a
std::string temporary. Then it adds the other char array to the std::string
temporary. This whole thing is in parenthesis so I can get a const char*
using .c_str() which outfile needs.

Quite a mess isn't it?

There are other ways to do it. You can go the old C style way and use
sprintf into a char buffer, but the you have to worry that the char buffer
is big enough to hold the entire string.

There are also different ways to convert from a number to a char array or a
std::string, that is just my favorite way.

I'm sure you'll get other responses to your post with other ways to do it.
Make sure you understand what everyone is saying and pick the way that suits
you best, but learn from all of them.
 
M

Markus Schoder

Tilman said:
Hi Felix,


#include <sstream>

std::eek:stringstream filename;
filename << "../gamefiles/rooms/"
<< r.getRoomNumber() << ".room";
std::eek:fstream outfile(filename.str().c_str());

Or just

ofstream outfile((string("../gamefiles/rooms/") +
r.getRoomNumber() + ".room").c_str());
 
F

Felix85

thanks for all the help it worked. thanks for the explanation Jim. it
helped alot.
 
M

Marcus Kwok

Markus Schoder said:
Or just

ofstream outfile((string("../gamefiles/rooms/") +
r.getRoomNumber() + ".room").c_str());

Except this won't work, as there is no overload of operator+() that
takes a string and an int.
 
M

Markus Schoder

Marcus said:
Except this won't work, as there is no overload of operator+() that
takes a string and an int.

You have not looked closely. r.getRoomNumber() does not return an int.
From the original post:

54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");

room.cpp:54: error: invalid operands of types `const char*' and `const
char[6]' to binary `operator+'
 
J

Jonathan Mcdougall

Markus said:
Marcus said:
Except this won't work, as there is no overload of operator+() that
takes a string and an int.

You have not looked closely. r.getRoomNumber() does not return an int.
From the original post:

54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");

room.cpp:54: error: invalid operands of types `const char*' and `const
char[6]' to binary `operator+'

This is probably because

1) "../gamefiles/rooms/" is a const char[20] but decayed to const char*
2) the getRoomNumber() returns an int (const char* + int = const char*)
3) we get to add a const char* to ".room" which is a const char[6],
which is illegal

Although only the OP can confirm this


Jonathan
 
M

Markus Schoder

Jonathan said:
Markus said:
Marcus said:
Or just

ofstream outfile((string("../gamefiles/rooms/") +
r.getRoomNumber() + ".room").c_str());

Except this won't work, as there is no overload of operator+() that
takes a string and an int.

You have not looked closely. r.getRoomNumber() does not return an int.
From the original post:

54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");

room.cpp:54: error: invalid operands of types `const char*' and `const
char[6]' to binary `operator+'

This is probably because

1) "../gamefiles/rooms/" is a const char[20] but decayed to const char*
2) the getRoomNumber() returns an int (const char* + int = const char*)
3) we get to add a const char* to ".room" which is a const char[6],
which is illegal

Although only the OP can confirm this

I stand corrected.
 
L

Liang Li

Felix85 said:
53 void room2File(room r){
54 ofstream outfile("../gamefiles/rooms/" +

I remember boost provides a class lexical_cast, maybe it can help you.
try this line code:
"../gamefiles/rooms/" +
boost::lexical_cast<std::string>(r.getRoomNumber()) + ".room"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,301
Latest member
BuyPureganics

Latest Threads

Top