LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to use the libiconv with a C program? (https://www.linuxquestions.org/questions/programming-9/how-to-use-the-libiconv-with-a-c-program-499391/)

mesh2005 11-07-2006 07:28 AM

How to use the libiconv with a C program?
 
I want to write a simple program in C that reads a file line by line and converts each line to UTF-8. The problem is I read the line in char buffer[256] and the iconv functions needs input and output buffers of the type char ** . I tried to cast but I always get segmentation fault. Could you please help?
Thank you

slanted 11-07-2006 06:46 PM

have you tryed
char **buffer;

buffer = (char **)malloc(sizeof(char *));
buffer[0] = (char *)malloc(sizeof(char) * 256);

then read data into buffer[0][i] instead of buffer[i] for example (this would be for reading char by char into the buffer)
dont forget to free(buffer[0]); free(buffer); when your done

that gives you a char **, sorry I have no clue about the iconv function though
but that may work, or atleast get you on the right track

paulsm4 11-07-2006 07:33 PM

Uh, no. The advice slanted gave isn't correct.

If you defined your input buffer as "char inbuf[256]" (for example), then you need to pass inbuf's *address* to iconv ()
Code:

  // EXAMPLE
  count = iconv(cd, &inbuf, &inbytesleft, &outbuf, &howMany);

'Hope that helps .. PSM

slanted 11-07-2006 07:57 PM

would that not just pass a pointer to the buffer (char *) when the function is asking for a pointer to a pointer to the buffer (char **)

maybe:
char **inptr;

inptr = (char **) &inbuf;

count = iconv(cd, inptr, etc, etc, etc);

mesh2005 11-15-2006 01:53 AM

I tried both but I got "Segmentation fault"

paulsm4 11-15-2006 09:35 AM

mesh2005 -

Here's the story:

1. You need to allocate an input buffer (for example, "char inbuf[256]")

2. You pass iconv the address of the buffer.
NOT the buffer (not "iconv (cd, inbuf, ...)"), but the ADDRESS OF inbuf.
This is where the "**" syntax comes from.

3. The REASON for this is that iconv is going to CHANGE the start address as it processes the buffer, moving the pointer forward.

4. To make this work, you really need TWO variables:
a) One variable for the buffer itself ("inbuf")
b) A second variable for the current position in the buffer (for example "char *inchar")
The code would look something like this:
Code:

#define BUFSIZE 256

char inbuf[BUFSIZE];
char *inchar = inbuf;
int count = iconv(cd, &inchar, ...)

5. You also need to do the same thing for a corresponding "outbuf".

Here's some excellent documentation that should help:
http://en.wikipedia.org/wiki/Iconv
http://docs.hp.com/en/B2355-90130/iconv.3C.html

linux_hy 11-15-2006 10:44 PM

I use the iconv too,and I have other problem
when I convert string from utf16 to utf8,the full-corner space isn't converted correctly.but I do it with command "iconv",it's right


All times are GMT -5. The time now is 07:32 PM.