Sign in to follow this  

Convert from std::strong to char*

This topic is 4545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My log class takes a char* but since I want to put in a little more than just text a stringstream was easier; std::stringstream asd; asd << "this went wrong " << "error"; Log::Write(asd); But there it complains about the third line, "cannot convert parameter 1 from 'std::stringstream' to 'char *'". How do I do that conversion? Or is there some better way to do it?

Share this post


Link to post
Share on other sites
I guessed that but then I get:

cannot convert parameter 1 from 'const char *' to 'char *'

[edit] The log class is taken here from gamedev, the function that performs the writing looks like this:


/************************************************************
This is a static function that actually writes a line in the log.
It flushes the log after every entry so that in a crash, the
log file will have the info it needs.

Input: line to log

Outputs: writes a line to the log file
************************************************************/

void Log::Write(char* input, ...)
{
va_list arg;
va_start(arg, input);
if(log_file)
{
vfprintf(log_file, input, arg);
fprintf(log_file,"\n");
//be sure to flush the file so that the info gets to disk
fflush(log_file);
}
va_end(arg);
}



I tried to convert it so it takes a std::string instead but I dont know what all the function does so I didnt edit anything.

Share this post


Link to post
Share on other sites
Since Log takes a char* instead of a const char*, this means that the function can modify the contents of the string. Because of this, you can't directly use the string returned by c_str( ), which must not be modified.

What you can do is create a modifiable buffer into which you dump the string data, and pass that buffer to the function.

char* buffer = new char[asd.str( ).size( )+1];
strcpy( buffer, asd.str( ).c_str( ) );
Log::Write( buffer );
delete [] buffer;

Share this post


Link to post
Share on other sites
Well - you'll probably have to make Log::Write accept const char * (second option is some kind of hackery with const_cast). It shouldn't make any difference, as long as you don't modify the string inside Log::Write.

EDIT: Your function should work fine, if you change it to void Log::Write(const char* input, ...)

Oxyd

Share this post


Link to post
Share on other sites
I would recomend reading up on const correctness, the C++ FAQ lite has a section on it here.

In this case I doubt your Log::Write function actually modifies the string in which case you need to have your Log::Write function take a const char*, like Oxyd said. If you need a modifiable char* string from a std::string you need to make which ToohrVyk showed.

Share this post


Link to post
Share on other sites

This topic is 4545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this