Unicode(UTF16)文件读写终极方案---用C++标准的字节流读取,转化为宽字符(wchar_t),再以字节流写入
2011-07-29 13:13
726 查看
用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;
}
这半年来经常要用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;
}
相关文章推荐
- Unicode(UTF16)文件读写终极方案---用C++标准的字节流读取,转化为宽字符(wchar_t),再以字节流写入
- Unicode(UTF16)文件读写终极方案---用C++标准的字节流读取,转化为宽字符(wchar_t),再以字节流写入
- Unicode(UTF16)文件读写终极方案
- 文件读取C++文件读写操作(二)逐字符读取文本和逐行读取文本
- 黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律
- java字节流和字符流操作文件,指定编码写入和读取,递归创建上层目录
- C++实现文件简单加密与解密(读取文件中的字符,对字符转换,然后写入,完成加密,解密反之)
- [C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
- 文件读写——以字符方式读取与写入文件
- [C/C++标准库]_[初级]_[读写中文路径的文件--写入unicode字符串]
- C++文件读写操作(二)逐字符读取文本和逐行读取文本
- [C++]读写txt文件和从txt文件中读取数据写入数组中
- C++文件读写操作(一) 逐字符读取文件
- 标准c++ 读写unicode文件
- C/C++ 各种字符编码字符串的转换 可用于XML文件的字符转化读写 (收集 GBK - UTF8 - Unicode - ANSI )
- C++的标准库函数默认都是操作字节,而不是字符,非常痛苦,所以引入了u16string和u32string(Linux上的wchar_t是32位的原因,utf16对unicode的支持是有缺陷的)good
- C++ 读写文件实例总结 (StringToken 为单位读取 以及char为单位读取)
- C++ 标准文件流以 StringToken 为单位读取文件并全部刷新重写
- C++ 简单读写文本文件、统计文件的行数、读取文件数据到数组
- 探寻C++最快的读取文件的方案 C++ IO优化