LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Regarding Problem writing UTF8 characters to a file (https://www.linuxquestions.org/questions/programming-9/regarding-problem-writing-utf8-characters-to-a-file-724138/)

Sathya 05-06-2009 09:18 AM

Regarding Problem writing UTF8 characters to a file
 
Hi

My application running in Linux connects to informix database and fetches data from a TEXT column. The column content has some extended characters. For ex: XXX³X and AAA’A. The two characters ³ and ’ have value 179 and 146 which i found by printing in the code as well as from the website.
Please refer to the below line for the number and characters against them. http://www.tony-franks.co.uk/UTF-8.htm

LANG and LC_CTYPE env variables for my terminal session are set as follows.
LANG=en_US.UTF-8 and LC_CTYPE=en_US.UTF-8

My issue is:
When i print 146 using %c , instead of getting the expected ’ character i get only null character but character for 179 is printed properly and when i send both characters to file and do a cat of that file, the character for 179 is displayed properly. But when i do a vi of that file name, it is showing some junk.

Here is the program.
#include<stdio.h>
#include<wchar.h>
#include<locale.h>

int main()
{
FILE *fp=NULL;
fp=fopen("myfile.txt","w+");
setlocale(LC_CTYPE,"en_US.UTF-8");
unsigned char x=179;
unsigned char y=146;
printf("%c %d \n", x,x);
printf("%c %d \n", y,y);
fputc(x,fp);
fputc(y,fp);
return 0;
}

Output:
$./a.out
³ 179 ---> this is correct
146 ---> Unfortunately this is null character (not even a space)

$cat myfile.txt
³

Cat displays properly
But vi myfile.txt shows, ³<92>

Can you let me know what is the issue? why vi is not showing properly. I am not able migrate this data into a table in oracle also.
should i use iconv? If so how to use it.

when i say file myfile.txt
myfile.txt: Non-ISO extended-ASCII text, with no line terminators


Kindly help.

Thanks,
Sathya


All times are GMT -5. The time now is 10:21 PM.