平时经常批量处理文件,需要从unicode 转 gbk,或者是gbk转unicode,手动转太麻烦了,又没有linux系统,perl又是对UTF-8比较适合,还是自己弄个程序吧
这个程序就是 将 in 文件夹下的所有文件从A编码转换成B编码,转换后的文件放到 out 文件夹下,文件名不变。0 代表 unicode2gbk,1 代表 gbk2unicode
wfopen有warning,但是改成_wfopen_s又运行不对,就暂时这样吧
#include "stdafx.h"
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define LEN 1024
#define READLEN 1024
// 深度优先递归遍历目录中所有的文件
BOOL unicode2gbk(FILE *fp_in,FILE*fp_out);
BOOL gbk2unicode(FILE *fp_in,FILE*fp_out);
BOOL DirectoryList(LPCWSTR Path,LPCWSTR out_Path,char mode);
BOOL unicode2gbk(FILE *fp_in,FILE*fp_out)
{
wchar_t inbuffer[READLEN];
char outbuffer[READLEN*2];
int num=0;
size_t readlen=0;
fseek(fp_in,2,0);
while(fgetws(inbuffer,READLEN,fp_in)){
num = WideCharToMultiByte(CP_ACP,0,inbuffer,-1,NULL,0,NULL,0);
WideCharToMultiByte(CP_ACP,0,inbuffer,-1,outbuffer,num,NULL,0);
fwrite(outbuffer,sizeof(char),num-1,fp_out);
}
return true;
}
BOOL gbk2unicode(FILE *fp_in,FILE*fp_out)
{
char inbuffer[READLEN];
wchar_t outbuffer[READLEN];
int num=0;
size_t readlen=0;
setlocale(LC_ALL,"Chinese_China");
wchar_t tag=0xfeff;
fwrite(&tag,sizeof(wchar_t),1,fp_out);
while(fgets(inbuffer,READLEN,fp_in)){
num = MultiByteToWideChar(CP_ACP,0,inbuffer,-1,NULL,0);
MultiByteToWideChar(CP_ACP,0,inbuffer,-1,outbuffer,num);
fwrite(outbuffer,sizeof(wchar_t),num-1,fp_out);
}
return true;
}
BOOL DirectoryList(LPCWSTR Path,LPCWSTR out_Path,char mode)
{
errno_t err;
WIN32_FIND_DATA FindData;
HANDLE hError;
int FileCount = 0;
wchar_t FilePathName[LEN];
// 构造路径
wchar_t FullPathName[LEN];
wchar_t OutPathName[LEN];
wcscpy_s(FilePathName, Path);
wcscat_s(FilePathName, L"\\*.*");
hError = FindFirstFile(FilePathName, &FindData);
if (hError == INVALID_HANDLE_VALUE)
{
printf("搜索失败!");
return 0;
}
while(::FindNextFile(hError, &FindData))
{
// 过虑.和..
if (strcmp((char*)FindData.cFileName, ".") == 0|| strcmp((char*)FindData.cFileName, "..") == 0 )
{
continue;
}
// 构造完整路径输入路径,输出路径
wsprintf(FullPathName, L"%s\\%s", Path,FindData.cFileName);
wsprintf(OutPathName, L"%s\\%s", out_Path,FindData.cFileName);
FileCount++;
// 输出本级的文件,运行到哪个文件
wprintf(L"\n%d %s ", FileCount, FullPathName);
//转换文件
FILE * fp_in;
FILE *fp_out;
if((fp_in = _wfopen(FullPathName,L"rb")) == NULL)
{
printf("cannot open this file %s\n",FullPathName);
return false;
}
if((fp_out = _wfopen(OutPathName,L"wb")) == NULL)
{
printf("cannot open this file %s\n",OutPathName);
return false;
}
//转换函数
if (mode==0)//unicode2gbk
{
unicode2gbk(fp_in,fp_out);
}else if (mode==1)//gbk2unicode
{
gbk2unicode(fp_in,fp_out);
}
fclose(fp_in);
fclose(fp_out);
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
printf("<Dir>");
DirectoryList(FullPathName,OutPathName,mode);
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DirectoryList(L"in",L"out",0);
//DirectoryList(L"out",L"in",1);
return 0;
}