Reputation: 17243
In order to find if a file exists, I want to use the GetFileAttributes
WinAPI function.
The function accepts a LPCSTR
argument. How can I convert my classic const char*
string to this type?
Please note, I'm using C, not C++. Is this the right way to go in C too?
Upvotes: 0
Views: 329
Reputation: 58617
It is GetFileAttributesA
(note the A
) which uses LPCSTR
. The wide character version is GetFileAttributesW
, and its argument is LPCWSTR
. The generic name GetFileAttributes
is a shim which will switch between these two functions at compile time; it is defined in terms of a TCHAR
typedef
(const
strings of which are LPCTSTR
). TCHAR
switches between CHAR
or WCHAR
based on whether you build the program for Unicode support.
If you have a const char *
input intended to be passed to GetFileAttributes
being compiled for Unicode, or to be passed to GetFileAttributesW
, then a conversion is needed from byte string to wide string.
It's best to avoid mixing wide and narrow strings in the entire program, if at all possible, to avoid the cumbersome conversions.
Upvotes: 2
Reputation: 25261
According to this Microsoft documentation page, LPCSTR is defined in WinNT.h as follows:
typedef __nullterminated CONST CHAR *LPCSTR;
This evaluates to const char *
.
So, you are essentially asking how to convert const char*
to itself. In other words, the answer to your question is that no conversion is required.
Regarding your question, there is no difference between C and C++. However, C++ offers additional ways of handling strings.
Upvotes: 2