HP C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index

2.6 Program Examples

This section gives some program examples that show how the I/O functions can be used in applications.

Example 2-1 shows the printf function.

Example 2-1 Output of the Conversion Specifications

/*      CHAP_2_OUT_CONV.C                               */ 
 
/* This program uses the printf function to print the   */ 
/* various conversion specifications and their effect   */ 
/* on the output.                                       */ 
 
/* Include the proper header files in case printf has   */ 
/* to return EOF.                                       */ 
 
#include <stdlib.h> 
#include <stdio.h> 
#include <wchar.h> 
 
#define WIDE_STR_SIZE 20 
 
main() 
{ 
    double val = 123345.5; 
    char c = 'C'; 
    int i = -1500000000; 
    char *s = "thomasina"; 
    wchar_t wc; 
    wchar_t ws[WIDE_STR_SIZE]; 
 
    /* Produce a wide character and a wide character string */ 
 
    if (mbtowc(&wc, "W", 1) == -1) { 
        perror("mbtowc"); 
        exit(EXIT_FAILURE); 
    } 
 
    if (mbstowcs(ws, "THOMASINA", WIDE_STR_SIZE) == -1) { 
        perror("mbstowcs"); 
        exit(EXIT_FAILURE); 
    } 
 
    /* Print the specification code, a colon, two tabs, and the  */ 
    /* formatted output value delimited by the angle bracket     */ 
    /* characters (<>).                                          */ 
 
    printf("%%9.4f:\t\t<%9.4f>\n", val); 
    printf("%%9f:\t\t<%9f>\n", val); 
    printf("%%9.0f:\t\t<%9.0f>\n", val); 
    printf("%%-9.0f:\t\t<%-9.0f>\n\n", val); 
 
    printf("%%11.6e:\t\t<%11.6e>\n", val); 
    printf("%%11e:\t\t<%11e>\n", val); 
    printf("%%11.0e:\t\t<%11.0e>\n", val); 
    printf("%%-11.0e:\t\t<%-11.0e>\n\n", val); 
 
    printf("%%11g:\t\t<%11g>\n", val); 
    printf("%%9g:\t\t<%9g>\n\n", val); 
 
    printf("%%d:\t\t<%d>\n", c); 
    printf("%%c:\t\t<%c>\n", c); 
    printf("%%o:\t\t<%o>\n", c); 
    printf("%%x:\t\t<%x>\n\n", c); 
 
    printf("%%d:\t\t<%d>\n", i); 
    printf("%%u:\t\t<%u>\n", i); 
    printf("%%x:\t\t<%x>\n\n", i); 
 
    printf("%%s:\t\t<%s>\n", s); 
    printf("%%-9.6s:\t\t<%-9.6s>\n", s); 
    printf("%%-*.*s:\t\t<%-*.*s>\n", 9, 5, s); 
    printf("%%6.0s:\t\t<%6.0s>\n\n", s); 
    printf("%%C:\t\t<%C>\n", wc); 
    printf("%%S:\t\t<%S>\n", ws); 
    printf("%%-9.6S:\t\t<%-9.6S>\n", ws); 
    printf("%%-*.*S:\t\t<%-*.*S>\n", 9, 5, ws); 
    printf("%%6.0S:\t\t<%6.0S>\n\n", ws); 
} 

Running Example 2-1 produces the following output:


$ RUN  EXAMPLE
%9.4f:          <123345.5000>
%9f:            <123345.500000>
%9.0f:          <   123346>
%-9.0f:         <123346   >
%11.6e:         <1.233455e+05>
%11e:           <1.233455e+05>
%11.0e:         <      1e+05>
%-11.0e:        <1e+05      >
%11g:           <     123346>
%9g:            <   123346>
%d:             <67>
%c:             <C>
%o:             <103>
%x:             <43>
%d:             <-1500000000>
%u:             <2794967296>
%x:             <a697d100>
%s:             <thomasina>
%-9.6s:         <thomas   >
%-*.*s:         <thoma    >
%6.0s:          <      >
%C:             <W>
%S:             <THOMASINA>
%-9.6S:         <THOMAS   >
%-*.*S:         <THOMA    >
%6.0S:          <      >
$ 

Example 2-2 shows the use of the fopen , ftell , sprintf , fputs , fseek , fgets , and fclose functions.

Example 2-2 Using the Standard I/O Functions

/*      CHAP_2_STDIO.C  */ 
 
/* This program establishes a file pointer, writes lines from   */ 
/* a buffer to the file, moves the file pointer to the second   */ 
/* record, copies the record to the buffer, and then prints     */ 
/* the buffer to the screen.                                    */ 
 
#include <stdio.h> 
#include <stdlib.h> 
 
main() 
{ 
    char buffer[32]; 
    int i, 
        pos; 
    FILE *fptr; 
 
    /*  Set file pointer.            */ 
    fptr = fopen("data.dat", "w+"); 
    if (fptr == NULL) { 
        perror("fopen"); 
        exit(EXIT_FAILURE); 
    } 
 
    for (i = 1; i < 5; i++) { 
        if (i == 2)     /*  Get position of record 2. */ 
            pos = ftell(fptr); 
        /*  Print a line to the buffer.  */ 
        sprintf(buffer, "test data line %d\n", i); 
        /*  Print buffer to the record.  */ 
        fputs(buffer, fptr); 
    } 
 
    /*  Go to record number 2.       */ 
    if (fseek(fptr, pos, 0) < 0) { 
        perror("fseek");        /*  Exit on fseek error. */ 
        exit(EXIT_FAILURE); 
    } 
 
    /*  Read record 2 in the buffer. */ 
    if (fgets(buffer, 32, fptr) == NULL) { 
        perror("fgets");        /*  Exit on fgets error.  */ 
        exit(EXIT_FAILURE); 
    } 
    /*  Print the buffer.       */ 
    printf("Data in record 2 is: %s", buffer); 
    fclose(fptr);       /*  Close the file.     */ 
} 

Running Example 2-2 produces the following result:


$ RUN  EXAMPLE
Data in record 2 is: test data line 2

The output to DATA.DAT from Example 2-2 is:


test data line 1 
test data line 2 
test data line 3 
test data line 4 

Example 2-3 Using Wide Character I/O Functions

/*      CHAP_2_WC_IO.C                                          */ 
 
/* This program establishes a file pointer, writes lines from   */ 
/* a buffer to the file using wide-character codes, moves the   */ 
/* file pointer to the second record, copies the record to the  */ 
/* wide-character buffer, and then prints the buffer to the     */ 
/* screen.                                                      */ 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <wchar.h> 
 
main() 
{ 
    char flat_buffer[32]; 
    wchar_t wide_buffer[32]; 
    wchar_t format[32]; 
    int i, 
        pos; 
    FILE *fptr; 
 
    /*  Set file pointer.            */ 
    fptr = fopen("data.dat", "w+"); 
    if (fptr == NULL) { 
        perror("fopen"); 
        exit(EXIT_FAILURE); 
    } 
 
    for (i = 1; i < 5; i++) { 
        if (i == 2)     /*  Get position of record 2. */ 
            pos = ftell(fptr); 
        /*  Print a line to the buffer.  */ 
        sprintf(flat_buffer, "test data line %d\n", i); 
        if (mbstowcs(wide_buffer, flat_buffer, 32) == -1) { 
            perror("mbstowcs"); 
            exit(EXIT_FAILURE); 
        } 
 
        /*  Print buffer to the record.  */ 
        fputws(wide_buffer, fptr); 
    } 
 
    /*  Go to record number 2. */ 
    if (fseek(fptr, pos, 0) < 0) { 
        perror("fseek");        /*  Exit on fseek error. */ 
        exit(EXIT_FAILURE); 
    } 
 
    /*  Put record 2 in the buffer.  */ 
    if (fgetws(wide_buffer, 32, fptr) == NULL) { 
        perror("fgetws");       /*  Exit on fgets error. */ 
        exit(EXIT_FAILURE); 
    } 
    /*  Print the buffer. */ 
    printf("Data in record 2 is: %S", wide_buffer); 
    fclose(fptr);               /*  Close the file.  */ 
} 

Running Example 2-3 produces the following result:


$ RUN  EXAMPLE
Data in record 2 is: test data line 2

The output to DATA.DAT from Example 2-3 is:


test data line 1 
test data line 2 
test data line 3 
test data line 4 

Example 2-4 shows the use of both a file pointer and a file descriptor to access a single file.

Example 2-4 I/O Using File Descriptors and Pointers

/*      CHAP_2_FILE_DIS_AND_POINTER.C                           */ 
 
/* The following example creates a file with variable-length    */ 
/* records (rfm=var) and the carriage-return attribute (rat=cr).*/ 
/*                                                              */ 
/* The program uses creat to create and open the file, and      */ 
/* fdopen to associate the file descriptor with a file pointer. */ 
/* After using the fdopen function, the file must be referenced */ 
/* using the Standard I/O functions.                            */ 
 
#include <unixio.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define ERROR 0 
#define ERROR1 -1 
#define BUFFSIZE 132 
 
main() 
{ 
    char buffer[BUFFSIZE]; 
    int fildes; 
    FILE *fp; 
 
    if ((fildes = creat("data.dat", 0, "rat=cr", 
                        "rfm=var")) == ERROR1) { 
        perror("DATA.DAT: creat() failed\n"); 
        exit(EXIT_FAILURE); 
    } 
 
    if ((fp = fdopen(fildes, "w")) == NULL) { 
        perror("DATA.DAT: fdopen() failed\n"); 
        exit(EXIT_FAILURE); 
    } 
    while (fgets(buffer, BUFFSIZE, stdin) != NULL) 
        if (fwrite(buffer, strlen(buffer), 1, fp) == ERROR) { 
            perror("DATA.DAT: fwrite() failed\n"); 
            exit(EXIT_FAILURE); 
        } 
 
    if (fclose(fp) == EOF) { 
        perror("DATA.DAT: fclose() failed\n"); 
        exit(EXIT_FAILURE); 
    } 
} 


Chapter 3
Character, String, and Argument-List Functions

Table 3-1 describes the character, string, and argument-list functions in the HP C Run-Time Library (RTL). Although further discussion follows in this chapter, see the Reference Section for more detailed information on each function.

Table 3-1 Character, String, and Argument-List Functions
Function Description
Character Classification
isalnum , iswalnum Returns a nonzero integer if its argument is one of the alphanumeric characters in the current locale.
isalpha , iswalpha Returns a nonzero integer if its argument is one of the alphabetic characters in the current locale.
isascii Returns a nonzero integer if its argument is any ASCII character.
iscntrl , iswcntrl Returns a nonzero integer if its argument is a control character in the current locale.
isdigit , iswdigit Returns a nonzero integer if its argument is a digit character in the current locale.
isgraph , iswgraph Returns a nonzero integer if its argument is a graphic character in the current locale.
islower , iswlower Returns a nonzero integer if its argument is a lowercase character in the current locale.
isprint , iswprint Returns a nonzero integer if its argument is a printing character in the current locale.
ispunct , iswpunct Returns a nonzero integer if its argument is a punctuation character in the current locale.
isspace , iswspace Returns a nonzero integer if its argument is a white-space character in the current locale.
isupper , iswupper Returns a nonzero integer if its argument is an uppercase character in the current locale.
iswctype Returns a nonzero integer if its argument has the specified property.
isxdigit , iswxdigit Returns a nonzero integer if its argument is a hexadecimal digit (0 to 9, A to F, or a to f).
Character Conversion
btowc Converts a one-byte multibyte character to a wide character in the initial shift state.
ecvt, fcvt, gcvt Converts an argument to a null-terminated string of ASCII digits and return the address of the string.
index, rindex Searches for a character in a string.
mblen , mbrlen Determines the number of bytes in a multibyte character.
mbsinit Determines whether an mbstate_t object decribes an initial conversion state.
mbstowcs Converts a sequence of multibyte characters into a sequence of corresponding codes.
toascii Converts its argument, an 8-bit ASCII character, to a 7-bit ASCII character.
tolower, _tolower, towlower Converts its argument, an uppercase character, to lowercase.
toupper, _toupper, towupper Converts its argument, a lowercase character, to uppercase.
towctrans Maps one wide character to another according to a specified mapping descriptor.
wcstombs Converts a sequence of wide-character codes corresponding to multibyte characters to a sequence of multibyte characters.
wctob Determines if a wide character corresponds to a single-byte multibyte character and returns its multibyte character representation.
wctomb Converts a wide character to its multibyte character representation.
wctrans Returns the description of a mapping, corresponding to specified property, that can be later used in a call to towctrans .
wctype Converts a valid character class defined for the current locale to an object of type wctype_t .
String Manipulation
atof Converts a given string to a double-precision number.
atoi, atol Converts a given string of ASCII characters to the appropriate numeric values.
atoll, atoq (ALPHA, I64) Converts a given string of ASCII characters to an __int64 .
basename Returns the last component of a path name.
dirname Reports the parent directory name of a file path name.
strcat, strncat, wcscat, wcsncat Appends one string to the end of another string.
strchr, strrchr, wcschr, wcsrchr Returns the address of the first or last occurrence of a given character in a null-terminated string.
strcmp, strncmp, strcoll, wcscmp, wcsncmp, wcscoll Compares two character strings and returns a negative, zero, or positive integer indicating that the values of the individual characters in the first string are less than, equal to, or greater than the values in the second string.
strcpy, strncpy, wcscpy, wcsncpy Copies all or part of one string into another.
strxfrm, wcsxfrm Transforms a multibyte string to another string ready for comparisons using the strcmp or wcscmp function.
strcspn, wcscspn Searches a string for a character that is in a specified set of characters.
strlen, wcslen Returns the length of a string of characters. The returned length does not include the terminating null character (\0).
strpbrk, wcspbrk Searches a string for the occurrence of one of a specified set of characters.
strspn, wcsspn Searches a string for the occurrence of a character that is not in a specified set of characters.
strstr, wcswcs Searches a string for the first occurrence of a specified set of characters.
strtod, wcstod Converts a given string to a double-precision number.
strtok, wcstok Locates text tokens in a given string.
strtol, wcstol Converts the initial portion of a string to a signed long integer.
strtoll, strtoq (ALPHA, I64) Converts the initial portion of a string to signed __int64 .
strtoul, wcstoul Converts the initial portion of a string to an unsigned long integer.
strtoull, strtouq (ALPHA, I64) Converts the initial portion of the string pointed to by the pointer to the character string to an unsigned __int64 .
String Handling---Accessing Binary Data
bcmp Compares byte strings.
bcopy Copies byte strings.
bzero Copies nulls into byte strings.
memchr, wmemchr Locates the first occurrence of the specified byte within the initial length of the object to be searched.
memcmp, wmemcmp Compares two objects byte by byte.
memcpy, memmove, wmemcpy, wmemmove Copies a specified number of bytes from one object to another.
memset, wmemset Sets a specified number of bytes in a given object to a given value.
Argument-List Handling---Accessing a Variable-Length Argument List
va_arg Returns the next item in the argument list.
va_count Returns the number of longwords (VAX ONLY) or quadwords (ALPHA ONLY) in the argument list.
va_end Finishes the va_start session.
va_start, va_start_1 Initializes a variable to the beginning of the argument list.
vfprintf, vprintf, vsprintf Prints formatted output based on an argument list.


Previous Next Contents Index