I prepared this program please go through it. i am running this program in linux with gcc compiler. i am giving input in the tmp.txt file with Telugu or Hindi characters.
But I am not getting proper output. I found 2 problems:
1. These characters are not converting to wide characters.
2. On the console characters are printing with some junk value.
Can any one help me to solve.
PHP Code:
#include<stdio.h>
#include<wchar.h>
void convertMultiBytes(wchar_t *wchar, char *mbyte);
main()
{
FILE *fp;
wchar_t *aa;
char c[100];
int l=0;
if((fp=fopen("tmp.txt", "r")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
fgets(c,25,fp);
l=strlen(c);
printf("%d",l);
aa=(wchar_t *)malloc((l+1)*4);
memset (aa, 0, (l + 1)*4);
convertMultiBytes(aa,c);
printf("Entered maultybyte string:%s\nConveted wild char string:%ls\n",c,aa);
}
void convertMultiBytes(wchar_t *wchar, char *mbyte)
{
int n;
char *end,*start;
n = strlen(mbyte) + 1;
/* convert multibyte string to wide character string */
if (mbstowcs(wchar,mbyte,n) == (size_t)-1)
{
printf("ERROR: Could not convert multibyte character to wide char code\n");
convertBytebyByte(wchar,mbyte,n);
}
}
void convertBytebyByte(wchar_t *wchar, char *mbyte, int n)
{
int mbcnt;
char *end,*start;
end = mbyte + n;
start = mbyte;
for(;start <= end;)
{
mbcnt = mbtowc(wchar,start,20);
if(mbcnt > 0)
{
start += mbcnt;
wchar++;
}
else
{
printf("Invalid multibyte character occured\n");
printf("Converting the invalid character to '?'\n");
*wchar = '?';
start++;
}
}
}

