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 |
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 |
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 |
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); |
I tried both but I got "Segmentation fault"
|
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 Here's some excellent documentation that should help: http://en.wikipedia.org/wiki/Iconv http://docs.hp.com/en/B2355-90130/iconv.3C.html |
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. |