char[4] to char[40]

This is a discussion on char[4] to char[40] within the C++ Programming forums, part of the General Programming Boards category; Hey all, im having this problem here (kind of n00b problem): i have a struct as follows: Code: typedef struct ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    char[4] to char[40]

    Hey all,

    im having this problem here (kind of n00b problem):

    i have a struct as follows:
    Code:
    typedef struct 
    { 
    	TCHAR strName[40]; 
    	TCHAR strPath[64]; 
        bool bStarted;
    } Program;
    and im trying to read a file that contains infos to put that into

    Program X[50];

    later on.

    now when im reading the file and trying to put the name specified in the file, like "asd", into the X program im always getting the error that i cant convert char[4] to char[40].

    X[i].strName = "asd";

    Any help is appreciated.

    thx

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You need to use strcpy. You also need to properly honour the fact that you're using TCHAR and wrap the string literal with TEXT(), like this: TEXT("asd"). You also should not use a type that's dependent on a compilation flag, such as TCHAR, for data stored in a file - one version of the program would be unable to read files generated by the other!
    Instead you should use WCHAR or wchar_t and convert to a different character set manually in ANSI versions of the program - use WideCharToMultiByte() for that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    ok thx for the quick reply gonna try that

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    another option is TCHAR * strName or String since this is c++

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    TCHAR* will cause memory troubles (you have to allocate a buffer and remember to deallocate it.)
    Both will not work if the Program struct is supposed to be written directly to the file using fwrite/ostream::write.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    using WCHAR works now with using strcpy and a reinterpret_cast to convert the WCHAR array to a char *

    Code:
    strcpy(reinterpret_cast<char *>(X[i].strPath), ((*(vIter2)).substr((*(vIter2)).find("=") + 1)).c_str());
    thx again

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    What is all this TCHAR bull anyhow? What's wrong with good ol' char* / char[] ? (Ok so I know what's wrong, but meh ). Let me guess, TCHAR is an MSVC specific re-def type thingy around char buffers? Eww.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    well, it's actually from the platform SDK, not the compiler library...

    from WTypes.h:
    Code:
    typedef wchar_t WCHAR;
    
    typedef WCHAR TCHAR;
    So a WCHAR is a TCHAR is a wchar_t, I guess someone just liked his capitals
    Of course these tricks save quite some work when moving from a 16 bit to a 32 bit environment, all you now need to do is change the typedef in one header file and recompile, instead of changing potentially hundreds of thousands of declarations.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by jwenting
    well, it's actually from the platform SDK, not the compiler library...

    from WTypes.h:
    Code:
    typedef wchar_t WCHAR;
    
    typedef WCHAR TCHAR;
    So a WCHAR is a TCHAR is a wchar_t.
    Wrong. If you care to take a closer look, you'll find that if the UNICODE macro is not defined, either WTypes.h isn't even included or TCHAR is a typedef for CHAR, which is a typedef for char.
    In addition, you'll find that
    Code:
    const wchar_t *ptr = "hello";
    doesn't compile, so if WCHAR == TCHAR, then the code wouldn't compile.

    And DON'T DON'T DON'T use wchar_t/char reinterpret_casts!!! You'll get nonsensical data.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yes the whole point of TCHAR is so if you have #define UNICODE, TCHAR will be a typedef for wchar_t (and the TEXT() macro will place the L prefix on strings enclosed in the macro) and if UNICODE is not defined, TCHAR will be a typedef for char (and the TEXT() macro will do nothing).

    Code:
    #ifdef UNICODE
    
    #define __TEXT(q) L##q
    #else
    #define __TEXT(q) q
    #endif
    
    #define TEXT(q) __TEXT(q)
    That's straight from winnt.h.
    Last edited by homeyg; 03-06-2006 at 08:23 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21