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

ofstream problems

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()[i];
63 } else { outfile << r.getRoomExits()[i] << "
"; }
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.

May 4 '06 #1
9 3603
Hi Felix,
54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");


#include <sstream>

std::ostringstream filename;
filename << "../gamefiles/rooms/"
<< r.getRoomNumber() << ".room";
std::ofstream outfile(filename.str().c_str());

Best regards,
Tilman

May 4 '06 #2

"Felix85" <a.******@gmail.com> wrote in message
news:11*********************@j73g2000cwa.googlegro ups.com...
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.
May 4 '06 #3
Tilman Kuepper wrote:
Hi Felix,
54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");


#include <sstream>

std::ostringstream filename;
filename << "../gamefiles/rooms/"
<< r.getRoomNumber() << ".room";
std::ofstream outfile(filename.str().c_str());


Or just

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

May 4 '06 #4
thanks for all the help it worked. thanks for the explanation Jim. it
helped alot.

May 4 '06 #5
Markus Schoder <a3*************@yahoo.de> wrote:
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 4 '06 #6
Marcus Kwok wrote:
Markus Schoder <a3*************@yahoo.de> wrote:
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+'

May 4 '06 #7
Markus Schoder wrote:
Marcus Kwok wrote:
Markus Schoder <a3*************@yahoo.de> wrote:
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
Jonathan

May 4 '06 #8
Jonathan Mcdougall wrote:
Markus Schoder wrote:
Marcus Kwok wrote:
Markus Schoder <a3*************@yahoo.de> wrote:
> 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.

May 5 '06 #9

Felix85 wrote:
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"

May 7 '06 #10

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

Similar topics

1
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
2
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str();...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
2
by: BCC | last post by:
Hi I have an application/project called HKDemo. In HKDemo.cpp, in the usual init stuff for the main class HKDemoApp, I can use the following code with no problems: ofstream out_file("test.txt",...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
0
by: bsruth | last post by:
I am writing a .NET wrapper for an unmanaged C++ library using managed C++. I am having an issue with an ofstream object used inside of the unmanaged code. Here is the code that is giving me the...
5
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files...
8
mikejfe
by: mikejfe | last post by:
I wrote a program to read a file, store that information into a 3d array, sort that array, and then write to a file. Should be straightforward. The problem I am running into is within the writing...
15
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.