如何在 Windows 上用 C 编写 unicode hello world [英] how to write unicode hello world in C on windows

查看:25
本文介绍了如何在 Windows 上用 C 编写 unicode hello world的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力让它发挥作用:

<预><代码>#define UNICODE#define _UNICODE#include int main(){wprintf(L"Hello World!\n");wprintf(L"£안, 蠀, ☃!\n");返回0;}

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

<前>C:\dev\unicodevs\unicodevs\Debug>unicodevs.exe你好,世界!┬ú∞C:\dev\unicodevs\unicodevs\Debug>

鉴于终端没有字体来呈现这些,我认为这是可以预料的.但让我感动的是,即使我尝试这样做:

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

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

<前>你好,世界!£

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

<前>1>------重建全部开始:项目:unicodevs,配置:调试Win32------1>删除项目'unicodevs'的中间和输出文件,配置'Debug|Win32'1>编译...1>main.c1>.\main.c(1):警告 C4005:'UNICODE':宏重新定义1> 命令行参数:参见之前的UNICODE"定义1>.\main.c(2) : 警告 C4005: '_UNICODE' : 宏重新定义1> 命令行参数 : 参见之前的 '_UNICODE' 定义1>注意:包含文件:C:\Program Files\Microsoft Visual Studio 9.0\VC\include\wchar.h1>注意:包括文件:C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtdefs.h1>注意:包含文件:C:\Program Files\Microsoft Visual Studio 9.0\VC\include\sal.h1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\sal.h(108):警告 C4001:使用了非标准扩展单行注释"1>注意:包含文件:C:\Program Files\Microsoft Visual Studio 9.0\VC\include\crtassem.h1>注意:包含文件:C:\Program Files\Microsoft Visual Studio 9.0\VC\include\vadefs.h1>注意:包含文件:C:\Program Files\Microsoft Visual Studio 9.0\VC\include\swprintf.inl1>注意:包含文件:C:\Program Files\Microsoft Visual Studio 9.0\VC\include\wtime.inl1>链接...1>嵌入清单...1>正在创建浏览信息文件...1>Microsoft 浏览信息维护实用程序版本 9.00.307291> 版权所有 (C) 微软公司.版权所有.1>构建日志保存在file://c:\dev\unicodevs\unicodevs\unicodevs\Debug\BuildLog.htm"1>unicodevs - 0 个错误,3 个警告========== 全部重建:1 成功,0 失败,0 跳过 ==========

关于我做错了什么的任何想法?ST 上的类似问题(例如:unicode hello world for C?)似乎请参阅 *nix 版本 - 据我所知 setlocale() 不适用于 Windows.

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

解决方案

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

#include #include int main(){文件*输出;char bom[] = "\xFF\xFE";wchar_t s[] = L"中文!";size_t c;out = fopen("out.txt", "w");如果(输出 == NULL){perror("out.txt");返回 1;}c = fwrite(bom, 1, 2, out);如果(c != 2){perror ("致命的写入错误.");fclose(out);返回2;}c = fwrite(s, sizeof(wchar_t), wcslen(s), out);if(c != wcslen(s)){perror ("致命的写入错误.");fclose(out);返回2;}fclose(out);返回0;}

im tyring to get this to work:


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

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

using visual studio 2008 express (on windows xp, if it matters). when i run this from the command prompt (started as cmd /u which is supposed to enable unicode ?) i get this:

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

which i suppose was to be expected given that the terminal does not have the font to render those. but what gets me is that even if i try this:

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

the file produced (even though its UTF-8 encoded) looks like:

Hello World!
壓

the source file itself is defined as unicode (encoded in UTF-8 without BOM). the compiler output when building:

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 ==========

any ideas on what am i doing wrong ? similar questions on ST (like this one: unicode hello world for C?) seem to refer to *nix builds - as far as i understand setlocale() is not available for windows.

i also tried building this using code::blocks/mingw gcc, but got the same results.

解决方案

It's not the writing (wprintf) that's the problem, it's the cmd redirection of output that's causing the problem. You can try testing by writing directly to file instead. In that case, you might then run into notepad (or rather Windows API function) not guessing correctly and interpreting your text as ASCII incorrectly if you're just writing a couple of words. In which case, you'll need to write the BOM characters into the file first as well.

#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;
}

这篇关于如何在 Windows 上用 C 编写 unicode hello world的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆