TEXT( ) function

Discussion in 'C++' started by Michael Benson, Aug 9, 2004.

  1. 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;
    }
     
    Michael Benson, Aug 9, 2004
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Aug 9, 2004
    #2
    1. Advertising

  3. Michael Benson

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Aug 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,046
    Evan Carew
    Jan 21, 2004
  2. komal
    Replies:
    6
    Views:
    1,430
    msalters
    Jan 25, 2005
  3. Replies:
    2
    Views:
    930
    Bengt Richter
    Aug 1, 2005
  4. Giannis Papadopoulos

    Function pointer to void function and int function

    Giannis Papadopoulos, Sep 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,244
    Barry Schwarz
    Sep 5, 2005
  5. leo
    Replies:
    1
    Views:
    288
    Bob Lehmann
    Dec 5, 2005
Loading...

Share This Page