LPCWSTR manipulation - anyone can help?

Started by
3 comments, last by chillypacman 16 years, 5 months ago
I was using strings for this but I now HAVE to use LPCWSTR (since strings don't seem to support japanese characters). What I need to do is split the filename extension from an LPCWSTR. For instance if I have LPCWSTR file = L"abc.txt" I need to take out the '.txt' at the end of it and just have L"abc". Any help would be appreciated :)
Advertisement
Search backwards for the '.' character, and set it to 0. (NUL)

Use whatever string search function you want, for example wcsrchr().

EDIT: Oh yeah, btw. You're question was an oxymoron. You can't manipulate a LPCWSTR. A LPCWSTR is a constant string. I.e.

typedef const WCHAR *LPCWSTR;

You can however manipulate a LPWSTR (the same as above, but non-const).

[Edited by - scorpion007 on November 21, 2007 4:39:09 AM]
I'm not sure what you mean, when I use wcsrch it returns the first character, but how do I set it to null in the LPCWSTR itself?
Firstly, it doesn't return the first character, it returns a pointer to the last occurrence of a given character, or NULL if it could not be found.

Secondly, you cannot modify a string pointed to by a constant pointer. The pointer must point to a mutable string. I.e. LPWSTR is mutable, LPCWSTR is const. Notice the 'C' in the latter one.

For example, if you to get trim the extension from the string "FILE.TXT" you can do something like the following:

WCHAR str[] = L"FILE.TXT";LPWSTR found = wcsrchr(str, '.'); // no need to use L on a single character.if (found) *found = 0; // set *found to the NUL terminator
yep that helped, thanks. I was having a bit of trouble with the conversions but I think I got it.

This topic is closed to new replies.

Advertisement