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;
}