Visual C + + since version 2005, the project's default character set property is changed to use the wide character set (Unicode), so the multibyte character set (ASCII) should not be used by default.
This change allows us to get the code from the Internet, the book is not properly compiled. For example, the following program, which is based on the console and obtains the absolute path to the file.
#include <windows.h>#include<stdio.h>int main () { char Szdir[max_path]; GetCurrentDirectory (max_path,szdir); printf ("The currentDirectory is%s", szdir); GetChar (); return 0 ;}
In the Visual C + + version after 2005, the program did not compile properly because Unicode was used by default.
There are many ways to solve this problem:
Method One: (recommended)
types, functions that use Unicode. In this example, the type char becomes TCHAR, the function printf is changed to wprintf, the string is preceded by an L, or is placed in parentheses in the _t (). As follows:
#include <windows.h>#include<stdio.h>int main () { TCHAR szdir[max_path]; GetCurrentDirectory (max_path,szdir); wprintf (L"The currentDirectory is%s", szdir); GetChar (); return 0 ;}
Method Two: (recommended)
Do not modify the source code, but to modify the compiler's settings.
Right-click "Project"-"Properties"-"General"-"character set" (changed from default Unicode character set to "not set" or "multibyte Character set")
Method Three: (Not recommended)
A function that changes the function to ASCII. If the getcurrentdirectory is changed to Getcurrentdirectorya.
In addition, the compiler cannot pass the error message that usually prompts for type conversion. For example, the hint cannot convert parameter 2 from "Char [260]" to "LPWSTR". If the char type string is cast to the LPWSTR type at this point, because each character in Unicode occupies 2 bytes, each character in ASCII occupies one byte, and the printf function outputs only the first character.
Unicode and ASCII for Visual Studio