By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

TEXT( ) function

P: n/a
Hello,

I am something of a windows API newbe and would apreciate any help
that you might be able to provide.

I am attempting to understand exactly what the TEXT() function does.
I have seen that it is defined as an unsigned short pointer...
presumably converting a text string into a wide char. Unfortunatly,
when i attempt to convert a string to a wide char the string gets
garbled and is unusable. Can anyone find a problem in the following
code segment? (The printf does not display 'x had a value of Today is
your lucky day!' for y.

Many thanks,

Michael Benson

int function(void)
{
char x[100];
TCHAR y[100];

sprintf(x,"Today is your lucky Day!");
_stprintf(y,TEXT("x had a value of %s\n"),x);
printf("X = %s\nY = %s\n\n",x,y);
return 0;
}
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Michael Benson wrote:
I am something of a windows API newbe and would apreciate any help
that you might be able to provide.

I am attempting to understand exactly what the TEXT() function does.
[...]


You're in a wrong newsgroup. You need comp.os.ms-windows.programmer
Jul 22 '05 #2

P: n/a
Michael Benson wrote:
I am something of a windows API newbe and would apreciate any help
that you might be able to provide.
TEXT() is on-topic on newsgroups that cover the Windows API. This newsgroup
is only qualified to answer platform-neutral questions.

TEXT("x") wraps either "x" or L"x", depending whether one of UNICODE or
_UNICODE are defined. No idea which; just enable them both.

Modern GUI code should use Unicode instead of a codepage, so its strings
should be L. TEXT() is a hack used to allow a program to compile with either
default 8 or 16 bit strings, and leads to a few abuses. Only use it when you
mean it.
I am attempting to understand exactly what the TEXT() function does.
I have seen that it is defined as an unsigned short pointer...
presumably converting a text string into a wide char. Unfortunatly,
when i attempt to convert a string to a wide char the string gets
garbled and is unusable. Can anyone find a problem in the following
code segment? (The printf does not display 'x had a value of Today is
your lucky day!' for y.

Many thanks,

Michael Benson

int function(void)
{
char x[100];
TCHAR y[100];

sprintf(x,"Today is your lucky Day!");
_stprintf(y,TEXT("x had a value of %s\n"),x);
With TEXT() set to produce 8-bit string literals, that will accidentally
work.

With TEXT() set to produce 16-bit string literals, the 't' in _stprintf()
will see L"x had a value of %s\n" correctly, but it will not magically
promote the 8-bit array x to a 16-bit array x.

What you are researching here is not character widths so much as typesafety.
The printf family of functions are not typesafe, because a compiler cannot
use means within the C++ language itself to match an expression code, %s, to
an argument of the correct type.

The fix:

TCHAR x[100];

Further questions about typesafety are on-topic here, and questions about
MS's usual level of it are on-topic on an MSDN newsgroup.
printf("X = %s\nY = %s\n\n",x,y);
return 0;
}


--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.