您的位置:首页 > 编程语言 > C语言/C++

Unicode(UTF16)文件读写终极方案---用C++标准的字节流读取,转化为宽字符(wchar_t),再以字节流写入

2014-11-03 15:11 896 查看
http://blog.csdn.net/harry_lyc/article/details/6643844

用C++做程序真够麻烦的,先不说各种数据类型搞的人头疼,再者读写各种编码的文本文件ansi、Unicode、UTF-8等等,让人头晕目眩的。

这半年来经常要用C++处理各种不同编码的文本文件,开始读取Unicode文件,一直很麻烦。用了wifstream,wsprintf等方法。都是不尽人意。

后来突然想何不自己节写个库函数自己用。后来就付诸行动,开始想着按行读取,怎想到行读取错误。后来才明白,Unicode(UTF16)文件都是2个字节,包括英文字符,只不过是到位填充为0罢了。明白这些,字节就按字节读取,自己判断换行符,并且实现了字节流到宽字节流的转换,实现了以前windows下复杂的库函数的基本功能,写入时按照字节流写入。用标准的C++读取、写入方法和数据类型,这样可以提供跨平台的重复使用。

时间比较紧,并且没有做过大规模测试,各位如果发现问题,请指正。

/********************************************************************
* Copyright (C) 2011 Li Yachao
* Contact: liyc@gmail.com or harry_lyc@foxmail.com
*
* Permission to use, copy, modify, and distribute this software for
* any non-commercial purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both
* that copyright notice.
* It is provided "as is" without express or implied warranty.
*
* Version: 0.1
* Last update: 2011-7-29
*********************************************************************/
#include<iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

std::wstring UTF16StringLineToWstring(std::string utf16line);
std::string UTF16WStringToString(std::wstring utf16wline);

int main(int argc, char * argv[])
{
/*Unicode编码文件输入*/
std::string infile = "input.txt";
std::string outfile = "out.txt";

ifstream infs16;/*读入文件流*/
infs16.open(infile.c_str(),ios::in | ios::binary);
if(!infs16.is_open())
{
std::cout << "Open error," <<infile<<std::endl ;
return 0;
}
ofstream outfs16;/*写入文件流*/
/*注意open函数的参数问题很重要,要不输出出错*/
outfs16.open(outfile.c_str(),ios::out | ios::binary);
if(!outfs16.is_open())
{
std::cout << "Open error," <<outfile<<std::endl ;
return 0;
}

const int clength = 3;
char cc[clength]={'\0'} ;/*当前读入的字符*/
char pc[clength]={'\0'} ;/*当前的前驱的字符*/
string line="";
while (infs16.read(cc,2))
{	/*一次读入两个字节*/
line += cc[0];
line += cc[1];
if((pc[0] == '\x0d') && (pc[1] == '\x00')
&&(cc[0] == '\x0a') &&( cc[1] == '\x00'))
{
/*换行符标志*/
/*Unicode文件的字节流转换为宽字符*/
std::wstring wstr = UTF16StringLineToWstring(line);
/*宽字节流转换为字节流,重新写入Unicode文件*/
std::string str  = UTF16WStringToString(wstr);
outfs16 <<str;/*写入文件*/
line.clear();
}
strcpy(pc,cc);/*保存当前两个字符的前驱字符*/
memset(cc,sizeof(char),clength);

}

infs16.close();
outfs16.close();
return 0;
}
/*
*************************************************
功能   :将UTF16(Unicode)编码文件的字节流转换为宽字节流
参数   : utf16line,Unicode编码文件的字节流
返回值 : result,宽字节流
-------------------------------------------------
备注   :
-------------------------------------------------
作者   :Li Yachao
时间   :2011-7-29
*************************************************
*/
std::wstring UTF16StringLineToWstring(std::string utf16line)
{
std::wstring result = L"";
for(int i=0;i<utf16line.length() -1;i+=2)
{
unsigned char c1 = utf16line[i];
unsigned char c2 = utf16line[i + 1];
unsigned short wc;
if(c2 == 0)
{
wc = c1 ;
}
else
{
wc = c2 ;
wc = wc << 8;
wc += c1;
}
result += wc;
}
return result;
}
/*
*************************************************
功能   :将UTF16(Unicode)编码的宽字节符流转换为字节流
参数   : utf16wline,UTF16(Unicode)编码的宽字节符
返回值 : result,字节流
-------------------------------------------------
备注   :
-------------------------------------------------
作者   :Li Yachao
时间   :2011-7-29
*************************************************
*/
std::string UTF16WStringToString(std::wstring utf16wline)
{
std::string result = "";
for(int i=0;i<utf16wline.length();i++)
{
wchar_t wc = utf16wline[i];
unsigned char c1 = wc&0x00ff;
unsigned char c2 =( wc >> 8 );
result += c1;
result += c2;
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐