C ++中的Unicode文件写入和读取? [英] Unicode File Writing and Reading in C++?
本文介绍了C ++中的Unicode文件写入和读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以提供一个简单的例子在Unicode文件中读取和写入Unicode字符?
Can anyone Provide a Simple Example to Read and Write in the Unicode File a Unicode Character ?
推荐答案
iconv
(链接)图书馆这是非常标准的。一个过于简单的程序是:
On linux I use the iconv
(link) library which is very standard. An overly simple program is:
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#define BUF_SZ 1024
int main( int argc, char* argv[] )
{
char bin[BUF_SZ];
char bout[BUF_SZ];
char* inp;
char* outp;
ssize_t bytes_in;
size_t bytes_out;
size_t conv_res;
if( argc != 3 )
{
fprintf( stderr, "usage: convert from to\n" );
return 1;
}
iconv_t conv = iconv_open( argv[2], argv[1] );
if( conv == (iconv_t)(-1) )
{
fprintf( stderr, "Cannot conver from %s to %s\n", argv[1], argv[2] );
return 1;
}
bytes_in = read( 0, bin, BUF_SZ );
{
bytes_out = BUF_SZ;
inp = bin;
outp = bout;
conv_res = iconv( conv, &inp, &bytes_in, &outp, &bytes_out );
if( conv_res >= 0 )
{
write( 1, bout, (size_t)(BUF_SZ) - bytes_out );
}
}
iconv_close( conv );
return 0;
}
这是过于简单的演示转换。在现实世界中,通常有两个嵌套循环:
This is overly simple to demonstrate the conversion. In the real world you would normally have two nested loops:
- 一个读取输入,因此当它超过BUF_SZ
- 一个将输入转换为输出。记住如果你从ascii转换为UTF-32LE,你会得到每个iunput字节为4个字节的输出。所以内循环将通过检查
conv_res
然后检查errno
来处理这个问题。
- One reading input, so handle when its more than BUF_SZ
- One converting input to output. Remember if you're converting from ascii to UTF-32LE you will end up with each iunput byte being 4 bytes of output. So the inner loop would handle this by examining
conv_res
and then checkingerrno
.
这篇关于C ++中的Unicode文件写入和读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文