Join 86,248 C++ Programmers. There are 2,214 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
I really want to convert from TCHAR to CHAR and the reason of that is because I need to write the data back to the file, but the data is written into the file producing extra space between letter which I believe TCHAR is the cause, so can I convert back to CHAR?
Some source codes mention about using memset() function and CT2A(), but when I use CT2A() it produces error due to the without declaring identifier. Maybe I'm missing the header file for CT2A? But I went to search for it, and I didn't see anyting about its header file.
WriteFile(hFile, &carriageRet1, sizeof(carriageRet1), &byteWritten, NULL); //add new line WriteFile(hFile, &carriageRet2, sizeof(carriageRet2), &byteWritten, NULL); //add new line
}
return 0; }
As you can on my code, I tried to use the memset() and strcpy with CT2A(), but it doesn't work since the error is about CT2A(), so I have no idea then.
The same happens for T2A, but by the way I'm running on WinCE.
You have been provided the information regarding the macro CT2A(). You do not need to use memset with this macro. Please read the documentation on how to use the macros.
As I stated you need to include atlbase.h and atlconv.h into your program. CT2A is defined in atlconv.h. Since you are using WinCE I don't know if this is available to you or not. You may still be able to then anyways by including the headers and telling your compiler where those headers are. If you can't include these headers for some reason, just find the header anyway and copy/paste their macro into your program
MSDN
QUOTE
When using an ATL string conversion macro, specify the USES_CONVERSION macro at the beginning of your function in order to avoid compiler errors. For example:
Copy Codevoid func( LPSTR lpsz ) { USES_CONVERSION; ... LPWSTR x = A2W(lpsz) // Do something with x ... }
I comment out the code block where it does the coversion, and compile it with the new added header file, but it has error in the atlconv.h file saying _ASSERTE is undeclared identifier. But this time the error is not from my programing though.
This post has been edited by turtleC++: 8 May, 2008 - 06:38 PM
// Changed swprintf to sprintf and removed _T() sprintf(szBuffHour, "%d", hours); sprintf(szBuffMinute, "%d", minutes); sprintf(szBuffTotalSeconds, "%d", totalSeconds);
// Changed LPSTR to char * and changed _tcslen() to strlen() char *charString = new char[strlen(szBuffHour) +1]; memset(charString, 0x00, strlen(szBuffHour) + 1);
// Removed CT2A() strcpy(charString, szBuffHour);
In the above code, I removed all of Microsoft's crappy defines, and replaced them with standard C. Since you only want to deal with ASCII, there is no point using Unicode.
Another way to correct this, is to just write your own CT2A() function, which isn't hard to do.
On a side note, I would highly recommend you don't use MS defines for basic types when ever possible. By using them, you are basically forcing anyone who uses your code to use the Microsoft compiler, which is not the best thing to do.
Here are a few of the common ones CHAR - char LONG - long INT - int SHORT - short DWORD - short WORD - char
// Changed swprintf to sprintf and removed _T() sprintf(szBuffHour, "%d", hours); sprintf(szBuffMinute, "%d", minutes); sprintf(szBuffTotalSeconds, "%d", totalSeconds);
// Changed LPSTR to char * and changed _tcslen() to strlen() char *charString = new char[strlen(szBuffHour) +1]; memset(charString, 0x00, strlen(szBuffHour) + 1);
// Removed CT2A() strcpy(charString, szBuffHour);
In the above code, I removed all of Microsoft's crappy defines, and replaced them with standard C. Since you only want to deal with ASCII, there is no point using Unicode.
Another way to correct this, is to just write your own CT2A() function, which isn't hard to do.
On a side note, I would highly recommend you don't use MS defines for basic types when ever possible. By using them, you are basically forcing anyone who uses your code to use the Microsoft compiler, which is not the best thing to do.
Here are a few of the common ones CHAR - char LONG - long INT - int SHORT - short DWORD - short WORD - char
Thank for following up this problem with me.
In the previous message you mentioned about reading the macro, is it the macro that you can define on top of your source code where it acts like a normal function, but not the same as normal function-more like an inline function, is that the macro you were talking about? If that is the macro, then I sortly know that concept.
Sometimes I wonder too about big cap letter and small cap letter of the identifier. People interchange use CHAR then char, and TCHAR. I'm getting confused since I'm new to win32. So from the message above, CHAR is the same as char?
For DWORD, is it character type or integer type? because I remember I can assign integer to it. Since it has the word WORD at the end of it, so I assume it's character type, right?
On a side note, I would highly recommend you don't use MS defines for basic types when ever possible. By using them, you are basically forcing anyone who uses your code to use the Microsoft compiler, which is not the best thing to do.
Of course, this isn't true. The typedefs that you list come from a header file that any compiler can compile. Is your anti-Microsoft attitude clouding your technical judgment?
QUOTE(turtleC++ @ 9 May, 2008 - 09:25 AM)
Sometimes I wonder too about big cap letter and small cap letter of the identifier. People interchange use CHAR then char, and TCHAR. I'm getting confused since I'm new to win32. So from the message above, CHAR is the same as char?
CHAR is the same as char, and WCHAR is the same as wchar_t. TCHAR is switchable; it is the same as CHAR if you don't have UNICODE defined, and is the same as WCHAR if you do have UNICODE defined.
QUOTE(turtleC++ @ 9 May, 2008 - 09:25 AM)
For DWORD, is it character type or integer type? because I remember I can assign integer to it. Since it has the word WORD at the end of it, so I assume it's character type, right?
Here, a "word" means a "Word of storage". In computers, a "word" is kind of slippery to define since its meaning changes. Neglecting the word's colorful history and meaning, it turns out that a DWORD on Win32 is a 32-bit unsigned integer. It's a numeric type, not a character type.
That's very great explaination, thanks. Now I know CHAR, char, TCHAR and DWORD.
What about bool and BOOL? because when I trace on both of them during the debug mode, they can store 0,1 for true and false, so are they interpreted the same way?
For the TRUE and true, I assume they have the same meaning in term of usage.
By the way, do you have any link or source where can I read more about other different data type because I did found a site from microsoft page, but they didn't give much detail on each data type, so to me they assume programmers should know this.
This post has been edited by turtleC++: 9 May, 2008 - 09:55 AM