Results 1 to 8 of 8

Thread: String Concatention Help

  1. #1

    Thread Starter
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    String Concatention Help

    Hey, i have this;

    Code:
    LPSTR	szCmdToDo;
    TCHAR	* pVarName;
    
    szCmdToDo = "Hello ";
    pVarName = "there";
    szCmdToDo  = lstrcat(szCmdToDo, pVarName);
    Now szCmdToDo = NULL;
    There's nothing saved in it. What went wrong??

    Also, what is the difference between all the different types of strings?? what one's are the best one's to use??

    Thanx
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  2. #2

    Thread Starter
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    ok...

    Code:
    TCHAR      * szCmdToDo;
    TCHAR	* pVarName;
    
    szCmdToDo = "Hello ";
    pVarName = "there";
    szCmdToDo  = lstrcat(szCmdToDo, pVarName);
    As I interpret it, szCmdToDo and pVarName can be either ASCII (8 bit) or UNICODE (16 bit), depending on the information passed to them. However, how can I tell which type they are, and how can I use concatention on them if they are 16 bit wide characters??
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    TCHAR is just a character type.

    You're not working with strings here, you're working with character arrays. The only way you can append to a char array is if you have something like the following:
    Code:
    TCHAR buffer[200];
    buffer[0] = L'\0';
    
    const TCHAR *other = L"Hello";
    
    lstrcat(buffer, other);
    I'd suggest using a wstring or string, if you can get away with using C++...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    ahh

    Trying to gather some meaning from this...

    what is the difference between

    Code:
    char * pVarName;
    and
    Code:
    string pVarname;
    ???

    And I'm what include files do i need to make string variables, cause I've got
    Code:
    #include <string>
    And it's telling me that string is 'underfined'

    Thanx parksie!!
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  5. #5

    Thread Starter
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    sorry for all the questions, but this is finally becoming clear.

    if i have
    Code:
    LPSTR szOne;
    LPSTR szTwo;
    
    szOne = "One, ";
    szTwo = "Two";
    strcat(szOne, szTwo);
    I get an error, how can I get around this?? any way using any method... (try and make sum1 stupid like me understand it)

    Thanx
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Lot of questions to answer here...

    You have two main errors:
    You're using memory that doesn't belong to you.
    You're mixing UNICODE and ANSI.

    Several explanations first:
    LPSTR is a typedef for char *
    LPWSTR is a typedef for wchar_t *
    LPTSTR is a typedef for TCHAR *
    TCHAR is a hybrid typedef. If the symbol UNICODE is defined it's a typedef for wchar_t. Else it's a typedef for char.

    You should never, never ever, assign any string literal ("Hello") to any of those types.
    It's a big failure of compilers to allow this.

    A literal is always of type const something.
    "Hello" is of type const char * (LPCSTR). L"Hello" is of type const wchar_t * (LPCWSTR). TEXT("Hello") or _T("Hello") is of type const TCHAR * (LPCTSTR).

    You need to include <tchar.h> to use the _T notation, but only <windows.h> for the TEXT notation.

    'x' is of type char, L'x' of type wchar_t and _T('x') or TEXT('x') of type TCHAR.

    You use strcmp or lstrcmpA for comparing char *, wcscmp or lstrcmpW for comparing wchar_t * and _tcscmp or lstrcmp for comparing TCHAR *.
    Other string functions have similar naming schemes (strcat, wcscat, _tcscat).

    Remember this stuff, it's the base of UNICODE/ANSI interoperability.

    By now you get compile errors: "Can't convert const char * to char *".
    Line: the strcat call.

    Have to use a buffer like parksie showed you:
    TCHAR buf[100];
    LPCTSTR str1 = _T("One, ");
    LPCTSTR str2 = _T("Two");
    _tcscpy(buf, str1);
    _tcscat(buf, str2);
    MessageBox(NULL, buf, buf, MB_OK);

    Reason:
    When you do
    LPCTSTR str1 = _T("One, ");
    Then there are two problems:
    str1 points to a fixed region in your exe where the string is stored. This region might be shared so you may not change it, which is one problem and the reason you should only assign string literals to const pointers, and there is exactly enough space to hold this string, no more, no less. Therefore you may not append another string to this.



    string is a C++ class. It is defined in the header <string> in the namespace std. There are three ways to use it:
    1) Direct referencing:
    std::string str1;
    std::string str2;
    2) Selective importing:
    using std::string;
    string str1;
    string str2;
    3) Importing the whole namespace:
    using namespace std;
    string str1;
    string str2;
    This also means that all other symbols in the namespace std are available (like cout, cin, fstream, ...).


    string allows you to forget all those things about static memory and such. It handles memory management automatically.

    You just do
    string str1("One, ");
    string str2("Two");
    str1 += str2;
    MessageBoxA(NULL, str1.c_str(), str2.c_str(), MB_OK);

    Note that I used MessageBoxA. This is the version that uses ANSI strings (MessageBoxW wants UNICODE (wchar_t *), MessageBox TCHAR *).

    wstring will use wchar_t instead of char. But what about TCHAR? There is no such thing in the standard library.

    But you can easily create one yourself:
    typedef basic_string<TCHAR> tstring;

    tstring str1, str2;
    str1 = _T("Hello");
    str2 = _T(", World!");
    ...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    ahhhhhhh

    I see...
    Thank you

    One more thing , in Win32 programming is there anyway of dealing with strings instead of character arrays?? I just worry that I'll declare a character array and it won't be big enough... Where as if u declare strings, they can be as big or as small as I need them.

    what if
    TCHAR buf[100];
    LPCTSTR str1 = _T("One, ");
    LPCTSTR str2 = _T("Two");
    _tcscpy(buf, str1);
    _tcscat(buf, str2);
    MessageBox(NULL, buf, buf, MB_OK);
    because of what the user may enter, 100 characters isn't enough..
    Last edited by Wak; Oct 28th, 2002 at 04:03 PM.
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You could count the resulting characters and dynamically allocate memory.

    Or you can simply use the string (wstring, tstring) class. Just note that there are some situations where you can't use them, especially for getting values from windows.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width