如何在Windows上用C编写unicode hello world

时间:2010-03-30 06:58:06

标签: c windows visual-studio-2008 unicode

为了让这项工作得以实现:


#define UNICODE
#define _UNICODE
#include <wchar.h>

int main()
{
    wprintf(L"Hello World!\n");
    wprintf(L"£안, 蠀, ☃!\n");
    return 0;
}

使用visual studio 2008 express(在windows xp上,如果重要的话)。 当我从命令提示符(从cmd / u启动,它应该启用unicode?)运行时,我得到这个:

C:\dev\unicodevs\unicodevs\Debug>unicodevs.exe
Hello World!
┬ú∞
C:\dev\unicodevs\unicodevs\Debug>

我认为假设终端没有用于呈现那些字体的字体。 但是,即使我尝试这个,我得到的是:

C:\dev\unicodevs\unicodevs\Debug>cmd /u /c "unicodevs.exe > output.txt"

生成的文件(即使其UTF-8编码)看起来像:

Hello World!
壓

源文件本身被定义为unicode(以UTF-8编码,没有BOM)。 构建时的编译器输出:

1>------ Rebuild All started: Project: unicodevs, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'unicodevs', configuration 'Debug|Win32'
1>Compiling...
1>main.c
1>.\main.c(1) : warning C4005: 'UNICODE' : macro redefinition
1>        command-line arguments :  see previous definition of 'UNICODE'
1>.\main.c(2) : warning C4005: '_UNICODE' : macro redefinition
1>        command-line arguments :  see previous definition of '_UNICODE'
1>Note: including file: C:\Program Files\Microsoft Visual Studio 9.0\VC\include\wchar.h
1>Note: including file:  C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdefs.h
1>Note: including file:   C:\Program Files\Microsoft Visual Studio 9.0\VC\include\sal.h
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\sal.h(108) : warning C4001: nonstandard extension 'single line comment' was used
1>Note: including file:   C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtassem.h
1>Note: including file:   C:\Program Files\Microsoft Visual Studio 9.0\VC\include\vadefs.h
1>Note: including file:  C:\Program Files\Microsoft Visual Studio 9.0\VC\include\swprintf.inl
1>Note: including file:  C:\Program Files\Microsoft Visual Studio 9.0\VC\include\wtime.inl
1>Linking...
1>Embedding manifest...
1>Creating browse information file...
1>Microsoft Browse Information Maintenance Utility Version 9.00.30729
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Build log was saved at "file://c:\dev\unicodevs\unicodevs\unicodevs\Debug\BuildLog.htm"
1>unicodevs - 0 error(s), 3 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

关于我做错什么的任何想法? 关于ST的类似问题(像这一个:unicode hello world for C?)似乎是指* nix版本 - 据我所知,setlocale()不适用于Windows。

我也尝试使用代码:: blocks / mingw gcc来构建它,但得到了相同的结果。

1 个答案:

答案 0 :(得分:5)

这不是写作(wprintf)的问题,而是输出的cmd重定向导致了问题。您可以通过直接写入文件来尝试测试。在这种情况下,如果您只是写了几个单词,那么您可能会遇到记事本(或者更确切地说是Windows API函数)没有正确猜测并将您的文本解释为ASCII错误。在这种情况下,您还需要首先将BOM字符写入文件。

#include <stdio.h>
#include <wchar.h>

int main()
{
    FILE *out;
    char bom[] = "\xFF\xFE";
    wchar_t s[] = L"中文!";
    size_t c;

    out = fopen ("out.txt", "w");
    if(out == NULL)
    {
        perror("out.txt");
        return 1;
    }

    c = fwrite(bom, 1, 2, out);
    if(c != 2)
    {
        perror ("Fatal write error.");
        fclose(out);
        return 2;
    }

    c = fwrite(s, sizeof(wchar_t), wcslen(s), out);
    if(c != wcslen(s))
    {
        perror ("Fatal write error.");
        fclose(out);
        return 2;
    }

    fclose(out);

    return 0;
}