I try to convert a UTF-16LE text file to ASCII using iconv
but for some reason my code just hangs forever, any idea what am I doing wrong?
#include <stdio.h>
#include <iconv.h>
#include <string.h>
#define S_SIZE (1024)
#define bool int
#define true 1
#define false 0
int main(){
iconv_t icd;
FILE *fp_src, *fp_dst;
char s_src[S_SIZE], s_dst[S_SIZE];
char *p_src, *p_dst;
size_t n_src, n_dst;
icd = iconv_open("ASCII", "UTF-16LE");
fp_src = fopen("utf8_test.txt", "rb");
fp_dst = fopen("ascii_test.txt", "w");
while(true){
fgets(s_src, S_SIZE, fp_src);
if (feof(fp_src))
break;
p_src = s_src;
p_dst = s_dst;
n_src = strlen(s_src);
n_dst = S_SIZE-1;
while(0 < n_src){
iconv(icd, &p_src, &n_src, &p_dst, &n_dst);
}
*p_dst = '\0';
fputs(s_dst, fp_dst);
}
fclose(fp_dst);
fclose(fp_src);
iconv_close(icd);
return 0;
}
Could it be because ASCII file is terminated in EOF and UTF-16LE in WEOF?