PDA

Click to See Complete Forum and Search --> : string manipulation


TnT
Oct 3rd, 2000, 11:42 AM
I need some help for string manipulation (without CString class).
I know basics of C++, but never used strings.
For example I need a function for appending a backslash after a directory
name:

VOID WINAPI AppendSlash(LPTSTR *lpFileName)
{
len = lstrlen(*lpFileName) + 1;
if (len > 0 && szBuffer[len] != '\\')
{
// Modify string by adding a backsslash
}
}

Thanks,
Jef Driesen

parksie
Oct 3rd, 2000, 02:24 PM
How 'bout this:

void WINAPI AppendSlash(LPTSTR *lpFileName) {
long lLength = _tcslen(lpFileName);
if(len > 0 && lpFileName[lLength-1] != '\\') {
lpFileName[lLength] = '\\';
}
}

This is the safest way, as long as you pass a buffer that's at least a character longer, unless it already has a backslash.
This is to try and cut down on pointer-related problems (you pass a pointer which has been supplied to something else. the pointer is reassigned, and gets invalidated :()

Although I would suggest the STL string class - it's fast and doesn't have allocation bugs (like CString)(although something like this in your code is suggested:)

#define String basic_string<TCHAR>

...then use String rather than string so that Unicode doesn't break your app.