C ++: VC6 to VC8: error C2664: "fopen": the parameter 1 cannot be converted from "CString" to "const char *"

Source: Internet
Author: User

In fact, other similar problems may occur, such:

Error C2664: "fopen": parameter 1 cannot be converted from "CString" to "const char *"

Error C2039: "strcpy": Not a member of "ATL: CStringT <BaseType, StringTraits>"

However, the program is compiled well in VC6, And the settings are different after research. In VC6, MBCS encoding is used by default, that is, multi-byte characters; while VC8 and VC7 are Unicode encoding by default, so...

What are the differences between the two types of codes? I have referenced an article on the Internet. The author is anonymous and can only thank you here:

Http://pc.nengbang.cn/group_thread/view/id-2603

In VC6, MBCS encoding is used by default, that is, multi-byte characters. Actually, ASCII code greater than 0x80 is supported. In this way, a text can be expressed as 2 bytes, and GB2312 is represented as this.

The default installation of VC6 does not contain UNICODE libraries. To write UNICODE programs in VC6, you must install the Unicode libraries of CRT and MFC.

To make your program support Unicode, remove the "_ MBCS" macro definition in your project properties and add the "UNICODE" and "_ UNICODE" macro definitions. (Note that both of them should be added, because CRT and MFC use UNICODE definition, while STL uses _ UNICODE)

If your program is MFC, the entry point of the Unicode version of the MFC Library is wWinMainCRTStartup.

To facilitate developers, VC6 provides Tchar. h, which defines some macros to help write code compatible with both codes.

Type

General text data types: _ UNICODE and _ MBCS: Undefined _ MBCS: defined _ UNICODE: defined

_ TCHAR char wchar_t
_ TINT int wint_t
_ TSCHAR signed char wchar_t
_ TUCHAR unsigned char wchar_t
_ TXCHAR char unsigned char wchar_t
Invalid _ T or _ TEXT (removed by Preprocessor) invalid (removed by Preprocessor) L (convert subsequent characters or strings to the corresponding Unicode form)

The related functions in CRT are in Tchar. replace str with _ tcs, for example, unsigned int strlen (const char *) in CRT *) now it is unsigned int _ tcslen (const TCHAR *). In Uniocde, It will be replaced with unsigned int _ wcslen (const wchar_t) *, while in MBCS, will be replaced with unsigned int _ mcslen (const char *).

It's easy to write Unicode and MBCS-compatible code. I have summarized some replacement rules.

1. Replace char with TCHAR (unsigned char must be removed from unsigned)

2. Replace str with _ tcs.

3 Add the _ T ("") macro to the definition of the String constant

4 The printf function family must be changed to wprintf. However, do not use the wprintf function to parse the char type.

In many cases, both Unicode and ASCII are required in the program. In this case, two APIs of the operating system are required.

WideCharToMultiByte is used to convert Unicode strings to MBCS

MultiByteToWideChar is used to convert MBCS strings to Unicode

Notes:

In Unicode encoding, sizeof is not so reliable. memset (0 sizeof () may make a big mistake and change it to memset (0 sizeof ()/szieof (TCHAR )) it's okay, huh, huh

In Unicode, a Chinese character is a character, len = strlen ()/2;

UNICODE programming with VC6

Recently, it takes a lot of effort to compile your program into a Unicode version. The related content is as follows, applicable to VC6, but VC7 and VC8 should also be similar (the latter creates a project by default and is compiled by Unicode ).

1. Added UNICODE and _ UNICODE pre-processing definitions.

Location: Project Settings-> C/C ++-> Preprocessor definitions

After these two definitions are added, some built-in types of MFC, such as TCHAR and CString, will be converted to support the wide character type (wchar_t)

2. Use the wide character type, such:

Char-> TCHAR, char *-> LPTSTR, const char *-> LPCTSTR

3. Use _ T () macro for string constants

4. Replace the string operation functions in the C library, such as strlen-> _ tcslen and strcmp-> _ tcscmp.

Similarly, there are conversion functions of strings and numbers in the C library, such as atoi-> _ ttoi, itoa-> _ itot, etc.

5. Set Project Settings-> li Nk-> Output-> Entry Point is set to wWinMainCRTSTartup

Otherwise, the following error occurs:
Msvcrtd. lib (crtexew. obj): error LNK2001: unresolved external symbol _ WinMain @ 16

6. the string in the C ++ Standard Library has the corresponding wide character version wstring, both of which are the special version of basic_string.

In StdAfx. h:

# Ifdef _ UNICODE
# Define tstring wstring
# Else
# Define tstring string
# Endif

Then use tstring in the Code, such as fstream/wfstream and ofstream/wofstream.

7. The English characters of the wide character version can still be directly compared with the integer value, for example:

CString s = _ T ("ABC ");
ASSERT (s [0] = 'A ');

8. If you still need to use ANSI strings, such as third-party class library interfaces, you can continue to use them. To convert Unicode strings and ANSI strings, you can use MultiByteToWideChar and WideCharToMultiByte.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.