如何在linux下使用unicode? [英] how to use unicode in c under linux?

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

问题描述

嗨所有

utf-8,但现在我需要在unicode中保存一个文件。我的linux是centos。

我知道这个系统支持unicode。 wchar_t * p是一个unicode

字符串,我打印len它是7.它是对的,我保存文件,

文件长度是7.我有检查一下,它是''hello ??''? = 0x3F,所以

这些代码出了什么问题?谢谢。

#define __STDC_ISO_10646__ 200104L

#include< wchar.h>

#include< stdio.h>

#include< stdlib.h>

#define _TEXT(x)L ## x

int main(){


FILE * fp = NULL;

wchar_t * filename = _TEXT(" oov.txt");

wchar_t * p = _TEXT("你好吗?o?");


wprintf(_TEXT("%S \ n)"),p);


wprintf(_TEXT("%d \ n"),wcslen(p));


fp = fopen(" oov.txt"," w");

fwprintf(fp,_TEXT("%S"),p);

fclose(fp);

返回0;

}


我的语言环境


LANG = zh_CN.UTF-8

LC_CTYPE =" zh_CN.UTF-8"

LC_NUMERIC =" zh_CN.UTF-8"

LC_TIME =" zh_CN.UTF-8"

LC_COLLATE =" zh_CN.UTF-8"

LC_MONETARY =" zh_CN.UTF-8"

LC_MESSAGES =&qu ot; zh_CN.UTF-8"

LC_PAPER =" zh_CN.UTF-8"

LC_NAME =" zh_CN.UTF-8"

LC_ADDRESS =" zh_CN.UTF-8"

LC_TELEPHONE =" zh_CN.UTF-8"

LC_MEASUREMENT =" zh_CN.UTF-8"

LC_IDENTIFICATION =" zh_CN.UTF-8"

LC_ALL =

解决方案


" flywav" < pp ******* @ gmail.comwrote in message fp => fopen(

" oov.txt"," w");


fwprintf(fp,_TEXT("%S"),p);

fclose(fp);



(fwprintf()以ASCII格式打印)


确保您的w_char类型实际上是多字节的。如果是,则fwprintf()

必须做错事。尝试以二进制文件打开文件。如果那个

失败了,你只需要接受该函数没有做你想要的b / b,并调用putc来写出Unicode字节字节。

-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


On 9 ??13è?,????5ê±58·?,Malcolm McLean < regniz .... @ btinternet.comwrote:


" flywav" < ppmsn2 ... @ gmail.comwrote in message fp => fopen(


" oov.txt"," w"); fwprintf(fp,_TEXT("%S"),p);


fclose(fp);



(fwprintf()用ASCII打印)


确保你的w_char类型实际上是多字节的。如果是,则fwprintf()

必须做错事。尝试以二进制文件打开文件。如果那个

失败了,你只需要接受该函数没有做你想要的b / b,并调用putc来写出Unicode字节字节。


-

免费游戏和编程好东西。 http://www.personal.leeds.ac.uk/~bgy1mm



谢谢,我检查过我的代码

i使用gcc -E 1.c

i找到代码:

typedef long int wchar_t;

所以我认为wchar_t是unicode。

#define __STDC_ISO_10646__ 200104L

#include< wchar.h>

#include< ; stdio.h>

#include< stdlib.h>

#define _TEXT(x)L ## x


int main(){


FILE * fp = NULL;

wchar_t * filename = _TEXT(" oov.txt");

wchar_t * p = _TEXT(" hello");


wprintf(_TEXT("%S \ n"),p);


wprintf(_TEXT("%d \ n"),wcslen(p));


fp = fopen(" oov.txt"," wb") ;

fwprintf(fp,_TEXT("%S"),p);

fclose(fp);

返回0; < br $>

}


文件长度仍为5. :(


flywav< ; pp ******* @ gmail.comwrites:


嗨所有

你知道unicdoe非常重要,在linux下,我总是使用

utf-8,但现在我需要在unicode中保存一个文件。我的linux是centos。

我知道这个系统支持unicode。 wchar_t * p是一个unicode

字符串,我打印len它是7.它是对的,我保存文件,

文件长度是7.我有检查一下,它是''hello ??''? = 0x3F,所以

这些代码出了什么问题?谢谢。



有一些问题。让我们来看看......


#define __STDC_ISO_10646__ 200104L



这是由实现设置的。你不能说!


#include< wchar.h>

#include< stdio.h>

#include< stdlib.h>

#define _TEXT(x)L ## x

int main(){


FILE * fp = NULL;

wchar_t * filename = _TEXT(" oov.txt");

wchar_t * p = _TEXT( 你好?? *?¥?";


wprintf(_TEXT("%S \ n)",p);



你不能打印宽的默认为stdout。此外,%S是非标准的
。这是一个错字吗?


您需要先调用setlocale,否则所有转换都不起作用。

之后,您需要决定是否需要byte或者宽输出。

字节输出更容易,但是如果你必须使用宽输出,那么你必须首先通过调用fwide来设置

< blockquote class =post_quotes>
wprintf(_TEXT("%d \ n"),wcslen(p));


fp = fopen(" oov .txt"," w");

fwprintf(fp,_TEXT("%S"),p);

fclose(fp);

返回0;

}



试试这个:


#include < wchar.h>

#include< stdio.h>

#include< stdlib.h>

#include< locale.h>


int main(无效)

{

setlocale(LC_ALL,"");

FILE * fp = fopen(" oov.txt"," w");

if(fp == NULL){

fprintf(stderr,Open failed.\ n);

返回EXIT_FAILURE;

}

如果(fwide(stdout,1)< 0 || fwide(fp,1)< 0){

fprintf(stderr,无法设置宽输出。\ n);

返回EXIT_FAILURE;

}


const wchar_t * p = L" hello ?? *?¥?" ;;

wprintf(L"%ls ​​\ n",p);

fwprintf(fp,L"%ls ​​\ n",p);

fclose(fp);

返回0;

}


如果你可以接受fprintf,你可以避开所有的fwide东西。


-

Ben。


hi all
you know unicdoe is very important, under linux, i always use
utf-8, but now i need save one file in unicode. my linux is centos.
and i know this system support unicode. the wchar_t *p is a unicode
string, i print the len it is 7. it is right,i save the file, the
file length is 7. i had checked it, it is''hello??'' ? = 0x3F, so
what''s wrong with these code? thank you.
#define __STDC_ISO_10646__ 200104L
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#define _TEXT(x) L ## x
int main() {

FILE *fp = NULL;
wchar_t *filename = _TEXT("oov.txt");
wchar_t *p = _TEXT("hello??o?");

wprintf(_TEXT("%S\n"), p);

wprintf(_TEXT("%d \n"), wcslen(p));

fp = fopen( "oov.txt", "w");
fwprintf(fp, _TEXT("%S"), p);
fclose(fp);
return 0;
}


my locale

LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=

解决方案


"flywav" <pp*******@gmail.comwrote in message fp = >fopen(
"oov.txt", "w");

fwprintf(fp, _TEXT("%S"), p);
fclose(fp);

(fwprintf() prints in ASCII)

Make sure your w_char type is actually multi-byte. If it is, then fwprintf()
must be doing the wrong thing. Try opening the file in binary. If that
fails, you''ll just have to accept that the function doesn''t do what you
want, and call putc to write out the Unicode byte by byte.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


On 9??13è?, ????5ê±58·?, "Malcolm McLean" <regniz....@btinternet.comwrote:

"flywav" <ppmsn2...@gmail.comwrote in message fp = >fopen(

"oov.txt", "w"); fwprintf(fp, _TEXT("%S"), p);

fclose(fp);


(fwprintf() prints in ASCII)

Make sure your w_char type is actually multi-byte. If it is, then fwprintf()
must be doing the wrong thing. Try opening the file in binary. If that
fails, you''ll just have to accept that the function doesn''t do what you
want, and call putc to write out the Unicode byte by byte.

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

thanks, i had check my code
i use gcc -E 1.c

i found the code:
typedef long int wchar_t;
so i think wchar_t is unicode.
#define __STDC_ISO_10646__ 200104L
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#define _TEXT(x) L ## x

int main() {

FILE *fp = NULL;
wchar_t *filename = _TEXT("oov.txt");
wchar_t *p = _TEXT("hello");

wprintf(_TEXT("%S\n"), p);

wprintf(_TEXT("%d \n"), wcslen(p));

fp = fopen( "oov.txt", "wb");
fwprintf(fp, _TEXT("%S"), p);
fclose(fp);
return 0;

}

the file length is still 5. :(


flywav <pp*******@gmail.comwrites:

hi all
you know unicdoe is very important, under linux, i always use
utf-8, but now i need save one file in unicode. my linux is centos.
and i know this system support unicode. the wchar_t *p is a unicode
string, i print the len it is 7. it is right,i save the file, the
file length is 7. i had checked it, it is''hello??'' ? = 0x3F, so
what''s wrong with these code? thank you.

There are a few things wrong. Lets have a look...

#define __STDC_ISO_10646__ 200104L

This is set by the implementation. You don''t get to say!

#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#define _TEXT(x) L ## x
int main() {

FILE *fp = NULL;
wchar_t *filename = _TEXT("oov.txt");
wchar_t *p = _TEXT("hello??*?¥?");

wprintf(_TEXT("%S\n"), p);

You can''t print "wide" to stdout by default. Also, %S is
non-standard. Is it a typo?

You need to call setlocale first or none of the conversions will work.
After that, you need to decide if you want byte or wide output.
Byte output is easier, but if you must use wide output, then you must
set that first with a call to fwide.

wprintf(_TEXT("%d \n"), wcslen(p));

fp = fopen( "oov.txt", "w");
fwprintf(fp, _TEXT("%S"), p);
fclose(fp);
return 0;
}

Try this:

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

int main(void)
{
setlocale(LC_ALL, "");
FILE *fp = fopen("oov.txt", "w");
if (fp == NULL) {
fprintf(stderr, "Open failed.\n");
return EXIT_FAILURE;
}
if (fwide(stdout, 1) < 0 || fwide(fp, 1) < 0) {
fprintf(stderr, "Failed to set wide output.\n");
return EXIT_FAILURE;
}

const wchar_t *p = L"hello??*?¥?";
wprintf(L"%ls\n", p);
fwprintf(fp, L"%ls\n", p);
fclose(fp);
return 0;
}

You can avoid all the fwide stuff if you fprintf is acceptable.

--
Ben.


这篇关于如何在linux下使用unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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