views:

199

answers:

4

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.

+3  A: 

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
dirkgently
I need a const char* in the end though.
Nick S.
@Nick S.: That's easy: `const char *s = append_number( ... ).c_str()`. Fill up the `...` with your own variables.
dirkgently
You can simply call `c_str()` at the end to get a `const char *`
Daniel DiPaolo
@dirkgently thanks, I forgot to add the extension fixed now. I'll try this.
Nick S.
`s<<sep<<x;` I guess it should be `s<<sep<<num`.
Cedric H.
@Cedric, that's what I thought, thanks.
Nick S.
@Cedric H.: Spot on! Virtual beer for you mate.
dirkgently
This still isn't working for me. I get this: - newfilepathname 0x007E97B0 "ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝ " char* in the watch window.
Nick S.
@Nick S.: Hm. There was a bug w.r.t extension stripping. Posted a minimal compilable example. Try that out.
dirkgently
@Nick S. Are you creating a separate declaration for the functions too? Then remove the `= ...` part from the definition of the functions.
dirkgently
The example I posted compiles w/o such warnings on GCC, Comeau and VS2010.
dirkgently
I got it to compile but I still get the same error with the YYYY's ugh.
Nick S.
@Nick S.:What compiler are you using? What inputs are you passing? You are not using wide character string literals, are you?
dirkgently
I'm using Visual Studio 2008. I think I've fond another way to do it using strings.
Nick S.
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
Thanks for your help. I ended up using strings and appended() and inserted() not elegant but it works.
Nick S.
A: 
 ostringstream oss;
 oss << FilePathName << "_" < id;
 const char* FilePathNameID = oss.str().c_str();
aeh
This doesn't work.
Nick S.
@Nick: whats the problem
aeh
A: 

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.

korbes
A: 

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.

Cyrus
I think this is where I'm having trouble.
Nick S.
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