LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2006, 07:28 AM   #1
mesh2005
Member
 
Registered: Sep 2005
Location: Ägypten
Distribution: Ubuntu 5.10
Posts: 155

Rep: Reputation: 30
Smile 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
 
Old 11-07-2006, 06:46 PM   #2
slanted
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Rep: Reputation: 0
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

Last edited by slanted; 11-07-2006 at 06:49 PM.
 
Old 11-07-2006, 07:33 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 11-07-2006, 07:57 PM   #4
slanted
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Rep: Reputation: 0
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);

Last edited by slanted; 11-07-2006 at 07:58 PM.
 
Old 11-15-2006, 01:53 AM   #5
mesh2005
Member
 
Registered: Sep 2005
Location: Ägypten
Distribution: Ubuntu 5.10
Posts: 155

Original Poster
Rep: Reputation: 30
I tried both but I got "Segmentation fault"
 
Old 11-15-2006, 09:35 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by paulsm4; 11-15-2006 at 01:11 PM.
 
Old 11-15-2006, 10:44 PM   #7
linux_hy
Member
 
Registered: Oct 2006
Posts: 66

Rep: Reputation: -2
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I uninstalled libiconv? libiconv.so.2: cannot open shared object file abefroman Linux - Software 2 11-03-2006 03:07 PM
Installed libiconv-1.9.2 XaViaR SUSE / openSUSE 1 07-31-2006 10:07 AM
where can i get libiconv.so.2 piggysmile Red Hat 5 05-28-2004 02:07 AM
Can't find libiconv.so.2, which package contains it? ICO Slackware 3 02-12-2004 01:36 AM
libiconv errors kalpana Linux - Newbie 0 07-22-2002 08:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration