Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting char* array from c++ stringstream ostringstream

I am trying to copy ostringstream to a char* array and am hoping someone can help me with understanding where my mistake lies. I looked on the forum, found some things that are similar and unfortunately am still am unable to get property copy from ostringstream to char*. In short I am trying to copy into the char* via:

ostringstream bld
bld<<"test"<<"is"<<"good"
const char * result = bld.str().c_str();

The complete code to reproduce an error is below. In this code I am having two functions that essentially build strings via ostringstream. In the makeFilePath() function I build a complete file path (ie /path/file.txt). In the add() function, I simply add two more char* arrays to an argument to get prefix /path/file.txt suffix.

The problem is for some unknown reason to me the fullFilePath also changes to look like prefix /path/file.txt suffix. Last three lines of a code will exhibit that.

I spent hours on this, thinking maybe this is a referencing issue or something else. However, none that I attemped worked. Any ideas how to get over this problem?

Thanks!!

#include <iostream>
#include <sstream>
#include <string>

using std::cout;
using std::endl;
using std::string;

using std::ostringstream;

const char* add(const char *fileName) {
    ostringstream bld;
    const char *prefix = "prefix";
    const char *suffix = "suffix";
    bld << prefix << " " << fileName << " " << suffix;
    string temp = bld.str();
    cout << "in add(): \n\t" << temp << endl;
    return temp.c_str();
}

const char * makeFilePath(const char *path, const char *name, const char *ext =
        ".txt") {
    ostringstream bld;
    bld << path << name << ext;
    cout << "makeFilePath(), returning: \n\t" << bld.str()<< endl;
    string temp = bld.str();
    return temp.c_str();

}

int main(int argc, char **argv) {
    cout << "=== PROJECT START ===" << endl;

    const char * filePath = "\\Path\\";
    const char *fileName = "FILENAME";
    const char *fullFilePath = makeFilePath(filePath, fileName);

    cout << fullFilePath before calling add():\n\t" << fullFilePath << endl;    
    const char* str = add(fullFilePath);
    cout << fullFilePath after calling add():\n\t" << fullFilePath << endl;

    return 1;
}
like image 637
JavaFan Avatar asked Dec 29 '25 13:12

JavaFan


1 Answers

The short answer is you need to use something like strdup to fix this:

const char* makeFilePath(const char *path, const char *name, const char *ext = ".txt")
{
  ostringstream bld;
  bld << path << name << ext;
  cout << "makeFilePath(), returning: \n\t" << bld.str()<< endl;

  return strdup(bld.str().c_str());
}

This is a really sub-optimal solution as now you have a memory leak unless you properly free the result of this function, plus it might return NULL on occasion which could cause chaos if you don't test for it. It'd be much better to return std::string.

If you're using C++, use C++.

like image 199
tadman Avatar answered Jan 01 '26 01:01

tadman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!