PMH4514
asked on
stripping a string from a TCHAR
I populate a TCHAR with a value from the registry:
TCHAR lpszDbPath[MSGBUFSIZ];
// registry key setup and error checking code omitted for clarity
RegQueryValueEx(hKey, _T("DBQ"), 0, &dwType, (BYTE *)lpszDbPath, &dwDataSize)) )
That gives me a value like the following in lpszDbPath
e:\MyDatabase\MyDB.mdb
what's the TChar way to strip the 'MyDB.mdb' from that result"?
thanks
TCHAR lpszDbPath[MSGBUFSIZ];
// registry key setup and error checking code omitted for clarity
RegQueryValueEx(hKey, _T("DBQ"), 0, &dwType, (BYTE *)lpszDbPath, &dwDataSize)) )
That gives me a value like the following in lpszDbPath
e:\MyDatabase\MyDB.mdb
what's the TChar way to strip the 'MyDB.mdb' from that result"?
thanks
You can use _tcsrchr, to do a reverse search for the back slash character, and set the result to zero.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you don't want to include the back slash character in the stripping, set position one of the result to 0.
TCHAR * pos = _tcsrchr(lpszDbPath, '\\');
if (pos)
pos[1] = 0;
TCHAR * pos = _tcsrchr(lpszDbPath, '\\');
if (pos)
pos[1] = 0;
ASKER
I'm not sure I understand how that works..
when lpszDbPath = "e:\MyDatabase\MyDB.mdb"
and I execute:
TCHAR * pos = _tcsrchr(lpszDbPath, '\\');
pos ends up equal to "\MyDB.mdb"
I want pos = "E:\MyDatabase\"
when lpszDbPath = "e:\MyDatabase\MyDB.mdb"
and I execute:
TCHAR * pos = _tcsrchr(lpszDbPath, '\\');
pos ends up equal to "\MyDB.mdb"
I want pos = "E:\MyDatabase\"
ASKER
oh wait, sorry Axter.. duh.. I gotta loop it.. nevermind :)