append string an path split

Hi is there a better way to do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string Msg;
Msg = "Process: ";
Msg += pe32.szExeFile;
Msg += "\n";

Msg += "ProcessID: ";
TCHAR ProcessID[32];
sprintf_s(ProcessID, "%d", pe32.th32ProcessID);
Msg += ProcessID;
Msg += "\n";

Msg += "File: ";
string res = FilePath;
Msg += res.substr(res.find_last_of("\\") + 1);
Msg += "\n";


also is ProcessID way right to convert from DWORD?
i was using Msg += pe32.th32ProcessID was giving errors sometimes
an is res.substr(res.find_last_of("\\") + 1) the best way to just add the filename?

Thanks
> is there a better way to do this?

Perhaps not better, but shorter.


> way right to convert from DWORD?

Yes. std::to_string() is simpler. // http://en.cppreference.com/w/cpp/string/basic_string/to_string


> is res.substr(res.find_last_of("\\") + 1) the best way to just add the filename?

A check that we have found a backslash would be prudent.
std::string::rfind() is simpler. // http://en.cppreference.com/w/cpp/string/basic_string/rfind

1
2
3
4
5
6
7
8
9
10
11
12
13
std::string Msg = "Process: " ;
Msg += pe32.szExeFile;

// http://en.cppreference.com/w/cpp/string/basic_string/to_string
Msg += "\n ProcessID: " + std::to_string( pe32.th32ProcessID ) + "\nFile: "

const std::string res = FilePath ;

// http://en.cppreference.com/w/cpp/string/basic_string/rfind
auto pos = res.rfind('\\') ;

if( pos != std::string::npos ) Msg += res.substr(pos+1) + '\n' ;
else Msg += res + '\n' ;


Thankyou this helped.
Topic archived. No new replies allowed.