Thread: how can i read a unicode char from a file ?

  1. #1
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70

    how can i read a unicode char from a file ?

    when i save a file as Unicode every char will be 2-byte long

    how can i read it from a file ??

    if i use for example

    Code:
    FILE *fp;
    c = getc(fp)
    the c will contain a one byte ??
    because i get it a character by character and long of the character just one byte !!!!!!!!!

    i want to read the character so it will be 2 byte

    cuz i want the program take the char as Unicode and convert it to something like ISO or Cp .. etc
    to be a 1-1, learn C

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    use fread() then

  3. #3
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    is there another way other than fread() ??
    to be a 1-1, learn C

  4. #4
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    there is an error when i use fread() , here`s the problem

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main(int argc,char *argv[])
    {
    
    	FILE *out;
    	FILE *in;
    	wchar_t *ch;
    
        if((ch=(wchar_t *)malloc(sizeof(wchar_t))) == NULL)
        {
            printf("Error: No enough memory\n");
            getchar();
            return 1;
        }
    	in = fopen("test.txt","rb");
    	out = fopen("output.txt","wb");
    	if(in == NULL)
    	{
    		printf("Can`t open file:%s\n",argv[1]);
    		exit(0);
    	}
    
    	while(fread(ch,sizeof(wchar_t),1,in) > 0)
    	{
    		printf("%d\n",ch);
    		fwrite(ch,sizeof(wchar_t),1,out);
    	}
    
    	fclose(in);
    	fclose(out);
    	return 0;
    }
    test.txt include the char
    Code:
    ب
    and if i read it and printf it using :
    Code:
    printf("%d\n",ch);
    it output this number :
    2824864
    why ??

    i am sure that the char
    Code:
    ب
    in unicode equal to 1576

    so the question is
    how could i print out the unicode decimal value of a char in a unicode text file ?
    to be a 1-1, learn C

  5. #5
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    Ops, sorry i found the problem ..

    the problem is that i print out the address looooolz... i forget to do this
    *ch

    thanks zacs for help
    to be a 1-1, learn C

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by c99
    7.24.3.1 The fgetwc function
    Synopsis
    1 #include <stdio.h>
    #include <wchar.h>
    wint_t fgetwc(FILE *stream);
    There are wide equivalents of most things in C99.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    thanks Salem ur the best here ! and i am sure that &#37;100 that you will answer my questions ..
    to be a 1-1, learn C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. cant read last char from a file
    By aze in forum C Programming
    Replies: 8
    Last Post: 04-25-2004, 05:20 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM