United States |
Previous | Contents | Index |
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); } } |
Table 3-1 describes the character, string, and argument-list functions in the Compaq C Run-Time Library (RTL). Although further discussion follows, see the Reference Section for more detailed information on each function.
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. |
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. |
wctob | Determines if a wide character corresponds to a single-byte multibyte character and returns its multibyte character representation. |
wcstombs | Converts a sequence of wide-character codes corresponding to multibyte characters to a sequence of multibyte characters. |
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 . |
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 ONLY) | 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 return 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 ONLY) | 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 ONLY) | 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 (VAX 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. |
The character-classification functions take a single argument on which they perform a logical operation. The argument can have any value; it does not have to be an ASCII character. The isascii function determines if the argument is an ASCII character (0 through 177 octal). The other functions determine whether the argument is a particular type of ASCII character, such as a graphic character or digit. The isw* functions test wide characters. Character-classification information is in the LC_CTYPE category of the program's current locale.
For all functions, a positive return value indicates TRUE. A return value of 0 indicates FALSE.
To briefly reference the character-classification functions in a subsequent table, each function is assigned a number, as shown in Table 3-2.
Function Number |
Function | Function Number |
Function |
---|---|---|---|
1 | isalnum | 7 | islower |
2 | isalpha | 8 | isprint |
3 | isascii | 9 | ispunct |
4 | iscntrl | 10 | isspace |
5 | isdigit | 11 | isupper |
6 | isgraph | 12 | isxdigit |
Table 3-3 lists the numbers of the functions (as assigned in Table 3-2) that return the value TRUE for each of the given ASCII characters. The numeric code represents the octal value of each of the ASCII characters.
ASCII Values |
Function Numbers |
ASCII Values |
Function Numbers |
---|---|---|---|
NUL 00 | 3,4 | @ 100 | 3,6,8,9 |
SOH 01 | 3,4 | A 101 | 1,2,3,6,8,11,12 |
STX 02 | 3,4 | B 102 | 1,2,3,6,8,11,12 |
ETX 03 | 3,4 | C 103 | 1,2,3,6,8,11,12 |
EOT 04 | 3,4 | D 104 | 1,2,3,6,8,11,12 |
ENQ 05 | 3,4 | E 105 | 1,2,3,6,8,11,12 |
ACK 06 | 3,4 | F 106 | 1,2,3,6,8,11,12 |
BEL 07 | 3,4 | G 107 | 1,2,3,6,8,11 |
BS 10 | 3,4 | H 110 | 1,2,3,6,8,11 |
HT 11 | 3,4,10 | I 111 | 1,2,3,6,8,11 |
LF 12 | 3,4,10 | J 112 | 1,2,3,6,8,11 |
VT 13 | 3,4,10 | K 113 | 1,2,3,6,8,11 |
FF 14 | 3,4,10 | L 114 | 1,2,3,6,8,11 |
CR 15 | 3,4,10 | M 115 | 1,2,3,6,8,11 |
SO 16 | 3,4 | N 116 | 1,2,3,6,8,11 |
SI 17 | 3,4 | O 117 | 1,2,3,6,8,11 |
DLE 20 | 3,4 | P 120 | 1,2,3,6,8,11 |
DC1 21 | 3,4 | Q 121 | 1,2,3,6,8,11 |
DC2 22 | 3,4 | R 122 | 1,2,3,6,8,11 |
DC3 23 | 3,4 | S 123 | 1,2,3,6,8,11 |
DC4 24 | 3,4 | T 124 | 1,2,3,6,8,11 |
NAK 25 | 3,4 | U 125 | 1,2,3,6,8,11 |
SYN 26 | 3,4 | V 126 | 1,2,3,6,8,11 |
ETB 27 | 3,4 | W 127 | 1,2,3,6,8,11 |
CAN 30 | 3,4 | X 130 | 1,2,3,6,8,11 |
EM 31 | 3,4 | Y 131 | 1,2,3,6,8,11 |
SUB 32 | 3,4 | Z 132 | 1,2,3,6,8,11 |
ESC 33 | 3,4 | [ 133 | 3,6,8,9 |
FS 34 | 3,4 | \ 134 | 3,6,8,9 |
GS 35 | 3,4 | ] 135 | 3,6,8,9 |
RS 36 | 3,4 | ^ 136 | 3,6,8,9 |
US 37 | 3,4 | -- 137 | 3,6,8,9 |
SP 40 | 3,8,10 | ?` 140 | 3,6,8,9 |
! 41 | 3,6,8,9 | a 141 | 1,2,3,6,7,8,12 |
" 42 | 3,6,8,9 | b 142 | 1,2,3,6,7,8,12 |
# 43 | 3,6,8,9 | c 143 | 1,2,3,6,7,8,12 |
$ 44 | 3,6,8,9 | d 144 | 1,2,3,6,7,8,12 |
% 45 | 3,6,8,9 | e 145 | 1,2,3,6,7,8,12 |
& 46 | 3,6,8,9 | f 146 | 1,2,3,6,7,8,12 |
' 47 | 3,6,8,9 | g 147 | 1,2,3,6,7,8 |
( 50 | 3,6,8,9 | h 150 | 1,2,3,6,7,8 |
) 51 | 3,6,8,9 | i 151 | 1,2,3,6,7,8 |
* 52 | 3,6,8,9 | j 152 | 1,2,3,6,7,8 |
+ 53 | 3,6,8,9 | k 153 | 1,2,3,6,7,8 |
' 54 | 3,6,8,9 | l 154 | 1,2,3,6,7,8 |
- 55 | 3,6,8,9 | m 155 | 1,2,3,6,7,8 |
?. 56 | 3,6,8,9 | n 156 | 1,2,3,6,7,8 |
/ 57 | 3,6,8,9 | o 157 | 1,2,3,6,7,8 |
0 60 | 1,3,5,6,8,12 | p 160 | 1,2,3,6,7,8 |
1 61 | 1,3,5,6,8,12 | q 161 | 1,2,3,6,7,8 |
2 62 | 1,3,5,6,8,12 | r 162 | 1,2,3,6,7,8 |
3 63 | 1,3,5,6,8,12 | s 163 | 1,2,3,6,7,8 |
4 64 | 1,3,5,6,8,12 | t 164 | 1,2,3,6,7,8 |
5 65 | 1,3,5,6,8,12 | u 165 | 1,2,3,6,7,8 |
6 66 | 1,3,5,6,8,12 | v 166 | 1,2,3,6,7,8 |
7 67 | 1,3,5,6,8,12 | w 167 | 1,2,3,6,7,8 |
8 70 | 1,3,5,6,8,12 | x 170 | 1,2,3,5,6,8 |
9 71 | 1,3,5,6,8,12 | y 171 | 1,2,3,5,6,8 |
: 72 | 3,6,8,9 | z 172 | 1,2,3,5,6,8 |
; 73 | 3,6,8,9 | { 173 | 3,6,8,9 |
< 74 | 3,6,8,9 | | 174 | 3,6,8,9 |
= 75 | 3,6,8,9 | } 175 | 3,6,8,9 |
> 76 | 3,6,8,9 | ?~ 176 | 3,6,8,9 |
? 77 | 3,6,8,9 | DEL 177 | 3,4 |
Previous | Next | Contents | Index |
|