Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

__FILE__ macro as wide string?

1,016 views
Skip to first unread message

Vladimir Cherepanov

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to
Just curious: is there any way to make the __FILE__ macro expand to a wide
character string?

John Kault

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to
No natural way that I know of. Simplest way is to convert on the fly but
this isn't recommended. That is, all strings in your app should normally be
of type TCHAR which resolves to "char" or "wchar_t" depending if your
compiling for Unicode or not. You can read up on all this if your not
already familiar. If you really have to do this anyway, you can do the
following:

#include <atlconv.h> // Defines the constructs used below

USES_CONVERSION; // Always required when using the macro below (or any of
its cousins)
const wchar_t *pFile = A2CW(__FILE__); // Creates a copy of its (ASCII)
arg in wide-character format and returns a pointer to it

"Vladimir Cherepanov" <nospam> wrote in message
news:u73mnNw...@cppssbbsa02.microsoft.com...

vlad

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to
#include <strstream>
using namespace std;

#define XX(st ) L##str
#define X(str) XX(str)

int main()
{
wcout << X(__FILE__) << endl;
return 0;
}

Vladimir

vlad

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to

token-pasting operator.

"Andreas Tuerk" <g...@to.hell> wrote in message
news:#dkZOKyWAHA.261@cppssbbsa04...
> What does ## mean?
>
> "vlad" <vl...@topproducer.com> schrieb im Newsbeitrag
> news:eypL4WxWAHA.251@cppssbbsa03...

Andreas Tuerk

unread,
Nov 30, 2000, 6:11:37 PM11/30/00
to

Bart Kowalski

unread,
Dec 1, 2000, 12:45:28 AM12/1/00
to
> No natural way that I know of. Simplest way is to convert on the fly but
> this isn't recommended. That is, all strings in your app should normally be
> of type TCHAR which resolves to "char" or "wchar_t" depending if your
> compiling for Unicode or not.

But __FILE__ is a compiler defined macro that simply expands to a string literal
and it is always of type char*, regardless of whether UNICODE is defined. Since
__FILE__ is just a string literal you can use a macro with a ## operator like
somebody else showed.


Bart.

John Kault

unread,
Dec 1, 2000, 3:00:00 AM12/1/00
to
> But __FILE__ is a compiler defined macro that simply expands to a string
literal
> and it is always of type char*, regardless of whether UNICODE is defined.
Since
> __FILE__ is just a string literal you can use a macro with a ## operator
like
> somebody else showed.

Your right, the macro is the better (faster) choice for a string literal
like this. However, unless there's some pressing need for wide characters
(not likely), he should really be using _T(__FILE__) which is what I
originally alluded to:

const TCHAR *pFile = _T(__FILE__);

0 new messages