C ++: vc6 to vc8 problem: Error c2664: "fopen": parameter 1 cannot be converted from "cstring" to "const char *" from http://www.cnblogs.c

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-> link-> output-> entry point 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.

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.