Click to See Complete Forum and Search --> : converting from LPWSTR to char *


nik88
July 17th, 2008, 07:53 PM
Hi, I am trying to convert an LPWSTR, specifically from an OPENFILENAME struct, to a char *.

so I have my code it looks like

OPENFILENAME ofn;

...

char *fname = (char *)ofn.lpstrFile; //this is what I tried and it didnt work


so pretty much I was wondering if there was some simple way of taking the file path from my OPENFILENAME struct and putting it in a useful char *.

0xC0000005
July 17th, 2008, 07:58 PM
You must have a UNICODE build if ofn.lpstrFile is LPWSTR. There is rarely a need for mixing char* and wchar_t* in the same build so can you explain why you need char*?

nik88
July 17th, 2008, 08:04 PM
well, I am using the file path that I got from ofn.lpstrFile to open a file using fopen. The first parameter of fopen is a char *. I am used to working with C, so I am not sure if this is the best way of doing this in c++.

sunil_cguru
July 18th, 2008, 12:59 AM
For wide char's with LPWSTR ..use _wfopen_s(..)

0xC0000005
July 18th, 2008, 04:39 AM
Virtually every c-runtime function, API function, etc. has a wide character equivalent. You should be using _wfopen() instead of fopen(). When developing your code you will quite often run into this problem. Assuming that you have help installed press F1 on fopen() (or any other function that takes a string parameter) and you will find the wide character equivalent.

nik88
July 18th, 2008, 06:09 PM
Thanks for the info, I am using the _wfopen_s instead of fopen.