[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Save file as UTF-8

Posted on 2010-11-17
9
Medium Priority
?
794 Views
Last Modified: 2012-08-13
Hi..

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.

Thanks!
#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;
             
    }
    else
    {
        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");

    while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
    {
        buff_ptr = buffer;
        while ((find_ptr = strstr(buff_ptr,text2find)))
        {
            while(buff_ptr < find_ptr)
                fputc((int)*buff_ptr++,fp2);

            fputs(text2repl,fp2);

            buff_ptr += find_len;
        }
        fputs(buff_ptr,fp2);
    }

    fclose(fp2);
    fclose(fp1);

    return 0;
}

Open in new window

0
Comment
Question by:vmorais_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 12

Accepted Solution

by:
HappyCactus earned 2000 total points
ID: 34162357
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/

0
 

Author Comment

by:vmorais_
ID: 34181322
Can't find how to install that library on Bloodshed DevC++.

I'm using Win32.
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 34182329
Have you tried this one? [1]


[1] http://sourceforge.net/projects/devpaks/files/
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vmorais_
ID: 34319284
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.
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 34319549
what is your problem? Can you post some code and some testing output?
0
 

Author Comment

by:vmorais_
ID: 34319892
for example, what parameters receive these functions?

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

iconv("ANSI", "UTF-8");
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 34319943
See this:

http://www.manpagez.com/man/3/iconv/
http://www.manpagez.com/man/3/iconv_open/

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.

0
 
LVL 40

Expert Comment

by:evilrix
ID: 34824220
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question