iconv_open

Allocates a conversion descriptor for a specified codeset conversion.

Format

#include  <iconv.h>

iconv_t iconv_open  (const char *tocode, const char
                    *fromcode);

Arguments

tocode
The name of the codeset to which characters are converted.
fromcode
The name of the source codeset. See Chapter 10 for information on obtaining a list of currently available codesets or for details on adding new codesets.

Return Values
A conversion descriptor. Indicates the call was successful. This descriptor is used in subsequent calls to iconv 
(iconv_t) -1  Indicates an error occurred. The function sets errno to one of the following:

  • EMFILE - The process does not have enough I/O channels to open a file.

  • ENOMEM - Insufficient space is available.

  • EINVAL - The conversion specified by fromcode and tocode is not supported.

  • EVMSERR - Nontranslatable VMS error occur. vaxc$errno contains the VMS error code. A value of SS$_BADCHKSUM in vaxc$errno indicates that a conversion table file was found, but its contents is corrupted. A value of SS$_IDMISMATCH in vaxc$errno indicates that the conversion table file version does not match the version of the C Run-Time Library.
 

Example

    #include <stdio.h>
    #include <iconv.h>
    #include <errno.h>
    
    int main ()
    {
        /*                                                                   */
        /* Declare variables to be used                                      */
        /*                                                                   */
        char fromcodeset[30];
        char tocodeset[30];
        int  iconv_opened;
        iconv_t iconv_struct;                   /* Iconv descriptor      */
    
        /*                                                                   */
        /* Initialize variables                                              */
        /*                                                                   */
        sprintf(fromcodeset,"DECHANYU");
        sprintf(tocodeset,"EUCTW");
        iconv_opened = FALSE;
    
        /*                                                                   */
        /* Attempt to create a conversion descriptor for the codesets        */
        /* specified. If the return value from iconv_open is -1 then         */
        /* an error has occurred. Check value of errno.                      */
        /*                                                                   */
        if ((iconv_struct = iconv_open (tocodeset, fromcodeset)) == (iconv_t)-1)
        {
            /*                                                               */
            /* Check the value of errno                                      */
            /*                                                               */
            switch (errno)
            {
            case EMFILE:
            case ENFILE:
              printf("Too many iconv conversion files open\n");
              break;
    
            case ENOMEM:
              printf("Not enough memory\n");
              break;
    
            case EINVAL:
              printf("Unsupported conversion\n");
              break;
    
            default:
              printf("Unexpected error from iconv_open\n");
              break;
            }
        }
        else
            /*                                                               */
     /* Successfully allocated a conversion descriptor   */
     /*         */
     iconv_opened = TRUE;
    
        /*                                                                   */
        /*  Was a conversion descriptor allocated                            */
        /*                                                                   */
        if (iconv_opened)
        {
            /*                                                               */
            /* Attempt to deallocate the conversion descriptor. If           */
            /* iconv_close returns -1 then an error has occurred.            */
            /*                                                               */
            if (iconv_close (iconv_struct) == -1)
            {
                /*                                                           */
                /* An error occurred. Check the value of errno               */
                /*                                                           */
                switch (errno)
                {
                case EBADF:
                    printf("Conversion descriptor is invalid\n");
                    break;
                default:
                    printf("Unexpected error from iconv_close\n");
                    break;
                }
            }
        }
        return(1);
    }
    


Previous Page | Next Page | Table of Contents | Index