I have const char* FilePathName
which looks like this: C:\ImportantFile.hex
And an int id = 12345;
I need to define a new const char* FilePathName_ID
that would append the id with an underscore to the original FilePathName
to look like this: C:\ImportantFile_12345.hex
I have looked at this but its different as I am using const char* which gives me the error cannot convert from 'const char * ' to 'char'
and need an underscore.
I need to end up with a const char*
EDIT I need to keep the file extension.

- 1
- 1

- 2,594
- 11
- 42
- 51
4 Answers
You need to create a new std::string
object or a null-terminated byte string. One easy way is this:
std::string append_number(std::string const& x, unsigned int num, char sep = '_') {
std::stringstream s;
s << strip_extension(x) << sep << num;
return s.str();
}
You can pass a string literal to the above function seamlessly.
Update: I notice that you probably also need to strip the extension:
std::string strip_extension(std::string x, char ext_sep = '.') {
return x.substr(0, x.find_last_of(ext_sep));
}
std::string get_extension(std::string const& x, char ext_sep = '.') {
return x.substr(x.find_last_of(ext_sep) + 1); // no error checking
}
See updated definition of append_number
.
Update 2: Try the following program:
#include <string>
#include <iostream>
#include <sstream>
std::string strip_extension(std::string const& x, char ext_sep = '.') {
return x.substr(0, x.find_last_of(ext_sep));
}
std::string append_number(std::string const& x, unsigned int num, char sep = '_') {
std::stringstream s;
s << strip_extension(x) << sep << num << '.' << get_extension(x);
return s.str();
}
int main() {
std::cout << append_number("file.hex", 45) << std::endl;
}
The output should be:
file_45.hex

- 108,024
- 16
- 131
- 187
-
I need a const char* in the end though. – Nick Sinas Sep 01 '10 at 16:02
-
@Nick S.: That's easy: `const char *s = append_number( ... ).c_str()`. Fill up the `...` with your own variables. – dirkgently Sep 01 '10 at 16:04
-
You can simply call `c_str()` at the end to get a `const char *` – Daniel DiPaolo Sep 01 '10 at 16:05
-
@dirkgently thanks, I forgot to add the extension fixed now. I'll try this. – Nick Sinas Sep 01 '10 at 16:12
-
1`s<
– Cedric H. Sep 01 '10 at 16:17 -
@Cedric H.: Spot on! Virtual beer for you mate. – dirkgently Sep 01 '10 at 16:18
-
This still isn't working for me. I get this: - newfilepathname 0x007E97B0 "ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ " char* in the watch window. – Nick Sinas Sep 01 '10 at 16:22
-
@Nick S.: Hm. There was a bug w.r.t extension stripping. Posted a minimal compilable example. Try that out. – dirkgently Sep 01 '10 at 16:31
-
@Nick S. Are you creating a separate declaration for the functions too? Then remove the `= ...` part from the definition of the functions. – dirkgently Sep 01 '10 at 16:40
-
The example I posted compiles w/o such warnings on GCC, Comeau and VS2010. – dirkgently Sep 01 '10 at 16:41
-
I got it to compile but I still get the same error with the YYYY's ugh. – Nick Sinas Sep 01 '10 at 16:46
-
@Nick S.:What compiler are you using? What inputs are you passing? You are not using wide character string literals, are you? – dirkgently Sep 01 '10 at 17:20
-
I'm using Visual Studio 2008. I think I've fond another way to do it using strings. – Nick Sinas Sep 01 '10 at 17:22
-
I'm not sure why you're hitting an error. Do check the updated source if you want to get the extension as well. – dirkgently Sep 01 '10 at 17:35
-
Thanks for your help. I ended up using strings and appended() and inserted() not elegant but it works. – Nick Sinas Sep 01 '10 at 19:11
ostringstream oss;
oss << FilePathName << "_" < id;
const char* FilePathNameID = oss.str().c_str();

- 779
- 5
- 8
As mikerobi said, you can use sprintf to deal with it:
char szAppended[MAX_PATH+10]; // ensure to have enough space for the path and ID
char szNoExt[MAX_PATH];
strcpy(szNoExt, FilePathName);
char *pExt = strrchr(szNoExt, '.');
*pExt = '\0';
sprintf(szAppended, "%s_%d.%s", szNoExt, id, pExt + 1);
You should probably also add some memory checks for the existence of extension.

- 1,273
- 1
- 12
- 18
Note: Any const char * returned via 'c_str()' from a std::string will only be valid as long as the std::string is in scope. You can obtain the pointer but after the string is gone it will be garbage.
You should be able to pass a char * into a function taking a const char * - it sounds like the primary issue may be creating/keeping a valid pointer.

- 583
- 6
- 9
-
-
A very hackish way to accomplish this would be to create a global instance of a std::string and use one of the solutions above to set its contents. After this the pointer should remain valid. However I would recommend trying to understand the scope problem, or possibly changing to use std::strings passed around by reference or value where appropriate. – Cyrus Sep 01 '10 at 16:57