• C

Save file as UTF-8


I have been trying to save a file as UTF-8 in C programming language but.. no chance. can not get it.

Can you please tell me how to do it? It is the only think that is missing to finish.

#include <stdio.h>
#include <string.h>

#define MAX_LEN_SINGLE_LINE     120   // ?

int main(int argc, char **argv[] )
     /*setlocale("LC_ALL", "pt_PT.utf8");*/
    if (argc < 3)
     printf("\n REPLACER_vM :: Deve especificar [ficheiro_alvo.ext] [ficheiro_destino.ext] \n");   
     return 0;
        printf("[ficheiro_alvo] %s \n", argv[1]);
        printf("[ficheiro_destino] %s \n", argv[2]);
    /* const char fileOrig[32] = "ficheiro1.csv"; */
    /* const char fileRepl[32] = "ficheiro2.idl"; */

    char fileOrig[99];
    strncpy( fileOrig, argv[1], 99 );
    char fileRepl[99];
    strncpy( fileRepl, argv[2], 99 );
    const char text2find[80] = ";";
    const char text2repl[80] = "|";

    char buffer[MAX_LEN_SINGLE_LINE+2];
    char *buff_ptr, *find_ptr;
    FILE *fp1, *fp2;
    size_t find_len = strlen(text2find);

    fp1 = fopen(fileOrig,"r");
    fp2 = fopen(fileRepl,"w");

        buff_ptr = buffer;
        while ((find_ptr = strstr(buff_ptr,text2find)))
            while(buff_ptr < find_ptr)


            buff_ptr += find_len;


    return 0;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You cannot this way, because C (and C++) only know about char format, not about charsets.
C in particular, does dial only with one-byte chars, and treat it as ASCII charsets.
You must use an external library such as libiconv[1].
Use iconv_open() to open the file, iconv() to make the conversion (if needed), write the new file and close it.
Pay attention that every time you want to refer to a character from code, you must use it with the right value within the charset selected.

[1] http://www.gnu.org/software/libiconv/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vmorais_Author Commented:
Can't find how to install that library on Bloodshed DevC++.

I'm using Win32.
Have you tried this one? [1]

[1] http://sourceforge.net/projects/devpaks/files/
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

vmorais_Author Commented:
Have just successfully installed using the given link.

I am trying to use iconv_open() and incov() in my code, but still can't get it.
what is your problem? Can you post some code and some testing output?
vmorais_Author Commented:
for example, what parameters receive these functions?

iconv_open("filename.ext"); | in this case iconv_open(argv[2])

iconv("ANSI", "UTF-8");
See this:


In brief, you create a translation table with iconv_open("ANSI","UTF-8"), then read the file in a buffer, and pass that buffer to iconv, then write the output buffer into the new file.

evilrixSenior Software Engineer (Avast)Commented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.