Thread: A few encoding and replacement questions...

  1. #1
    Registered User NaartjieDude's Avatar
    Join Date
    Apr 2010
    Posts
    4

    Question A few encoding and replacement questions...

    Hello there!
    This is my first post here on Cprogramming.com . Very nice site by the way.
    Anyhow, is there a method in C to convert a .txt document from ANSI encoding to Unicode?
    Then after that to take that document and replace x with y?
    Also, are the C documents themselves Unicode or something like that, or do I have to manually change to Unicode?

    EDIT: Sorry to be asking so many questions, but can you choose the .txt document with an Open dialog?

    Thanks!
    NaartjieDude
    Last edited by NaartjieDude; 04-30-2010 at 02:05 PM. Reason: Remembered something.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    What character encoding do you want to convert to? ASCII characters are encoded exactly the same way in UTF-8 as they are in ASCII, so if you're using UTF-8 you don't need to do anything.

    If you want to use something other than UTF-8, then iconv is probably your best option.

    > Also, are the C documents themselves Unicode or something like that, or do I have to manually change to Unicode?
    It depends on the default character encoding that your text editor uses. Advanced text editors like Vim and Emacs allows you to save a file in all kinds of different encodings.

  3. #3
    Registered User NaartjieDude's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Well, say that I have alot of text thats written in Polish, but is ALWAYS in ANSI. I can just change to Unicode. So I do that. But in ANSI all the Polish characters ( ś,ć,ę to give a few) turn to (³,œ,æ and so on).
    See what I mean? I can just go search and replace for EVERY Polish character, but it takes long and I wanna write this program as a challenge.
    SO can I replace œ,æ with ś,ć and so on? I use the GNU Compiler, so I tried :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        printf ("ąęóśćżłĄĘÓĆŚŻŁ");
    }
    http://img265.imageshack.us/img265/5...irdletters.png
    I also have Notepad++ if it makes a difference.
    Last edited by NaartjieDude; 05-01-2010 at 01:47 AM. Reason: mistake

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    > Well, say that I have alot of text thats written in Polish, but is ALWAYS in ANSI
    If the text is already in Polish (some kind of latin encoding I presume), then it's simply a matter of how you're displaying the text.

    For example, if you have a text file with polish characters and display it in a console that uses ASCII encoding, the characters will be interpreted as ASCII and thus come out wrong. If you change the character encoding in the console to match the one in the text file, everything will now display correctly.

  5. #5
    Registered User NaartjieDude's Avatar
    Join Date
    Apr 2010
    Posts
    4
    No, I want the program to load a text document, convert the ANSI characters to ISO 8859-2 or something with Polish characters, then save the document.
    http://en.wikipedia.org/wiki/Polish_alphabet

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int c;
    ...
    while( (c=fgetc( inputfile )) != EOF )
    {
        switch( c )
        {
            case 'x': fputc( 'y', outputfile ); break;
            ...
            default: fputc( c, outputfile );
        }
    }
    Replace 'x' with whatever you're looking for, and 'y' with whatever you're trying to put there instead.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User NaartjieDude's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Thanks!
    It gives a undeclared variable error, I suppose I have to fill in inputfile and outputfile?
    So then how do I choose what text document? So its dynamic, and shows a open dialog?
    Also what's with the
    . . .
    Last edited by NaartjieDude; 05-01-2010 at 03:50 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is pseudo-code. You fill in as many cases as there are for each thing you want to replace. Yes, you need to actually put some thought and work into it (not much actually, but some).


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. keyword replacement
    By JackBear in forum C Programming
    Replies: 11
    Last Post: 05-18-2007, 10:54 PM