Добавил:
Upload
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Информатика_1 / C / lecture11 / uni2ansi
.c/* This programm convert text file from unicode to ANSI text file
with codepage (866, 1251 ....) */
#include <stdio.h>
#include <windows.h>
#include <malloc.h>
void main(int argv,char *argc[] )
{
HANDLE hf1;
FILE *outfile;
DWORD size,sizer;
wchar_t *uni;
char *ansi;
unsigned int codepage;
printf("Unicode to ANSI transformation of text files\n Copyright BVV 2001\n");
if(argv!=4){perror("Error syntax:\n Syntax: unic2ansi infile outfile codepage\n");
}
//Open file argc[1] for read only
hf1=CreateFile(argc[1],GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hf1==0)perror(strcat("filed open",argc[1]));
// get size of file in bytes
size=GetFileSize((HANDLE)hf1,NULL);
if(size==-1)perror("Error getting file size");
//get memory for buffer
uni=malloc(size);
// read file hf1 to buffer uni
if(!ReadFile((HANDLE)hf1,uni,size,&sizer,NULL))perror("Error reading file");
//to proove Is unicode text file?
if(!IsTextUnicode(uni,size,NULL))perror("gm... It is not Unicode Text");
// get memory for buffer ansi for converted text
ansi=malloc(size/2);
// get codepage
codepage=(unsigned int)atoi(argc[3]);
// To convert Unicode text from buffer uni to ANSI text to buffer ansi
// with codepage codepage
WideCharToMultiByte(codepage,0,uni,size/2,ansi,size/2,NULL,NULL);
//write result file
outfile=fopen(argc[2],"wb");
fwrite(ansi,1,size/2,outfile);
fclose(outfile);
printf("done");
}
Соседние файлы в папке lecture11