typedef __mbstate_t | mbstate_t |
An object type other than an array type that can hold the conversion state information necessary to convert between sequences of (possibly multibyte) characters and wide-characters. If a codeset is being used such that an mbstate_t needs to preserve more than 2 levels of reserved state, the results are unspecified.
typedef __wint_t | wint_t |
An integral type capable of storing any valid value of wchar_t, or WEOF.
IMPORT_C wint_t | btowc | ( | int | ) |
The btowc function converts a single-byte character into a corresponding wide character. If the character is EOF , or not valid in the initial shift state, btowc returns WEOF.
The wctob function converts a wide character into a corresponding single-byte character. If the wide character is WEOF , or not able to be represented as a single byte in the initial shift state, wctob returns WEOF.
#include <wchar.h> // Illustrates how to use btowc API wint_t example_btowc(int c) { wint_t wc = L'a'; // converting single byte to wide-character wc = btowc(c); // return the character that was converted return (wc); }
#include <wchar.h> /* Illustrates how to use wctob API */ int example_wctob(void) { wint_t wc = L'a'; int c; /* represent a wide-char in a single byte*/ c = wctob(wc); /* return the single byte */ return(c); }
Limitations:
The current implementation of btowc and wctob is not affected by the LC_CTYPE category of the current locale. It works only for UTF8 character set.
Parameters | |
---|---|
Note: This description also covers the following functions - wctob() |
The fgetwc function obtains the next input wide character (if present) from the stream pointed at by stream, or the next character pushed back on the stream via ungetwc .
The getwc function acts essentially identical to fgetwc.
The getwchar function is equivalent to getwc with the argument stdin.
/* Illustrates how to use fgetwc API */ #include <stdio.h> #include <wchar.h> wint_t example_fgetwc(void) { FILE *fp = NULL; wint_t retval; /* opening the input file */ fp = fopen("input.txt","r"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* Read a character from the opened file */ retval = fgetwc(fp); /* Close the file open for reading */ fclose(fp); /* return the character read from the file */ return (retval); }
/* Illustrates how to use getwc API */ #include <stdio.h> #include <wchar.h> wint_t example_getwc(void) { FILE *fp = NULL; wint_t retval; /* opening the input file */ fp = fopen("input.txt","r"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* Read a character from the opened file */ retval = getwc(fp); /* Close the file open for reading */ fclose(fp); /* return the character read from the file */ return (retval); }
/* Illustrates how to use getwchar API */ #include <stdio.h> #include <wchar.h> wint_t example_getwchar(void) { wint_t retval; /* Read a character from standard input */ retval = getwchar(); /* return the character read */ return (retval); }
Parameters | |
---|---|
Note: This description also covers the following functions - getwc() getwchar() |
IMPORT_C wchar_t * | fgetws | ( | wchar_t * | , |
int | , | |||
struct __sFILE * | ||||
) |
The fgetws function reads at most one less than the number of characters specified by n from the given fp and stores them in the wide character string ws. Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained. If any characters are read, and there is no error, a '\0' character is appended to end the string.
#include <stdio.h> #include <wchar.h> /* Illustrates how to use fgetws API */ wchar_t *example_fgetws(wchar_t *buf) { FILE *fp = NULL; wint_t retval; int n; /* for example, 10 characters to be read */ n = 10; /* opening the input file */ fp = fopen("input.txt","r"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* Read characters from the opened file */ retval = fgetws(buf, n, fp); /* Close the file open for reading */ fclose(fp); /* return the character read from the file */ return (buf); }
The fputwc function writes the wide character wc to the output stream pointed to by stream.
The putwc function acts essentially identically to fputwc.
The putwchar function is identical to putwc with an output stream of stdout.
/* Illustrates how to use putwc API */ #include <stdio.h> #include <wchar.h> wint_t example_putwc(void) { FILE *fp = NULL; wchar_t wc = L'a'; wint_t rval; /* opening the file to write*/ fp = fopen("input.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* write a character into fp */ rval = putwc(wc, fp); /* Close the file opened for writing */ fclose(fp); /* return the value that was written */ return (rval); }
/* Illustrates how to use fputwc API */ #include <stdio.h> #include <wchar.h> wint_t example_fputwc(void) { FILE *fp = NULL; wchar_t wc = L'a'; wint_t rval; /* opening the file to write*/ fp = fopen("input.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* write a character into fp */ rval = fputwc(wc, fp); /* Close the file opened for writing */ fclose(fp); /* return the value that was written */ return (rval); }
/* Illustrates how to use putwchar API */ #include <stdio.h> #include <wchar.h> wint_t example_putwchar(void) { wint_t rval; wchar_t wc = L'q'; /* write a character onto the standard output */ rval = putwchar(wc); /* return the character that was written */ return (rval); }Output of putwchar
q
Parameters | |
---|---|
Note: This description also covers the following functions - putwc() putwchar() |
IMPORT_C int | fputws | ( | const wchar_t * | , |
struct __sFILE * | ||||
) |
The fputws function writes the wide character string pointed to by ws to the stream pointed to by fp.
#include <stdio.h> #include <wchar.h> /* Illustrates how to use fputws API */ int example_fputws(wchar_t *buf) { FILE *fp = NULL; int retval; /* open the file for writing*/ fp = fopen("write.txt","r"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* Write the characters into the file */ retval = fputws(buf, fp); /* Close the file open for writing */ fclose(fp); /* return the number of characters written */ /* into the file */ return (retval); }
IMPORT_C int | fwide | ( | struct __sFILE * | , |
int | ||||
) |
The fwide function determines the orientation of the stream pointed at by stream.
If the orientation of stream has already been determined fwide leaves it unchanged. Otherwise fwide sets the orientation of stream according to mode.
If mode is less than zero stream is set to byte-oriented. If it is greater than zero stream is set to wide-oriented. Otherwise mode is zero and stream is unchanged.
#include <stdio.h> #include <wchar.h> /* Illustrates how to use fwide API */ int example_fwide(void) { FILE *fp = NULL; int retval; int mode; /* open a file */ fp = fopen("input.txt","r"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* set the mode to wide*/ mode = 1; /* set the orientation of the file */ retval = fwide(fp, mode); /* check the return value */ if(retval == 1) { wprintf(L"Mode set to WIDE "); } else { wprintf(L"Error setting the mode to wide!! "); } /* set the mode to determine the orientation */ mode = 0; /* Read back the orientation that was set */ retval = fwide(fp ,mode); if(retval == 1) { wprintf("Mode not changed "); } else { wprintf("Error, mode changed!! "); } /* Close the file open for writing */ fclose(fp); /* return the mode of fp */ return (retval); }
See also: ferror() fgetc() fgetwc() fopen() fputc() fputwc() freopen()
IMPORT_C int | fwprintf | ( | struct __sFILE * | , |
const wchar_t * | , | |||
... | ||||
) |
The wprintf family of functions produces output according to a format as described below. The wprintf and vwprintf functions write output to stdout, the standard output stream; fwprintf and vfwprintf write output to the given output stream; swprintf , snwprintf , vswprintf and vsnwprintf write to the wide character string ws. These functions write the output under the control of a format string that specifies how subsequent arguments (or arguments accessed via the variable-length argument facilities of stdarg) are converted for output.
These functions return the number of characters printed (not including the trailing \0 used to end output to strings).
The swprintf , snwprintf , vswprintf and vsnwprintf functions will fail if n or more wide characters were requested to be written.
Note : 1. swprintf and snwprintf can be used interchangeably. 2. vswprintf and vsnwprintf can be used interchangeably. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the % character. The arguments must correspond properly (after type promotion) with the conversion specifier. After the %, the following appear in sequence:
An optional field, consisting of a decimal digit string followed by a $, specifying the next argument to access. If this field is not provided, the argument following the last argument accessed will be used. Arguments are numbered starting at 1. If unaccessed arguments in the format string are interspersed with ones that are accessed the results will be indeterminate. Zero or more of the following flags: '#' The value should be converted to an "alternate form". For c, d, i, n, p, s, and u conversions, this option has no effect. For o conversions, the precision of the number is increased to force the first character of the output string to a zero (except if a zero value is printed with an explicit precision of zero). For x and X conversions, a non-zero result has the string 0x (or 0X for X conversions) prepended to it. For a, A, e, E, f, F, g, and G conversions, the result will always contain a decimal point, even if no digits follow it (normally, a decimal point appears in the results of those conversions only if a digit follows). For g and G conversions, trailing zeros are not removed from the result as they would otherwise be. '0(zero)' Zero padding. For all conversions except n, the converted value is padded on the left with zeros rather than blanks. If a precision is given with a numeric conversion ( d, i, o, u, i, x, and X), the 0 flag is ignored. '-' A negative field width flag; the converted value is to be left adjusted on the field boundary. Except for n conversions, the converted value is padded on the right with blanks, rather than on the left with blanks or zeros. A - overrides a 0 if both are given. ' (space)' A blank should be left before a positive number produced by a signed conversion ( a, A, d, e, E, f, F, g, G, or i). '+' A sign must always be placed before a number produced by a signed conversion. A + overrides a space if both are used. '' Decimal conversions ( d, u, or i) or the integral portion of a floating point conversion ( f or F) should be grouped and separated by thousands using the non-monetary separator returned by localeconv. An optional decimal digit string specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given) to fill out the field width. An optional precision, in the form of a period . followed by an optional digit string. If the digit string is omitted, the precision is taken as zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s conversions. An optional length modifier, that specifies the size of the argument. The following length modifiers are valid for the d, i, n, o, u, x, or X conversion:
Modifier d, i o, u, x, X n hh signed char unsigned char signed char * h short unsigned short short * l (ell) long unsigned long long * ll (ell ell) long long unsigned long long long long * j intmax_t uintmax_t intmax_t * t ptrdiff_t (see note) ptrdiff_t * z (see note) size_t (see note) q (deprecated) quad_t u_quad_t quad_t *
Note: the t modifier, when applied to a o, u, x, or X conversion, indicates that the argument is of an unsigned type equivalent in size to a ptrdiff_t . The z modifier, when applied to a d or i conversion, indicates that the argument is of a signed type equivalent in size to a size_t . Similarly, when applied to an n conversion, it indicates that the argument is a pointer to a signed type equivalent in size to a size_t . The following length modifier is valid for the a, A, e, E, f, F, g, or G conversion:
Modifier a, A, e, E, f, F, g, G L long double
The following length modifier is valid for the c or s conversion: Modifier c s l (ell) wint_t wchar_t *
A character that specifies the type of conversion to be applied. A field width or precision, or both, may be indicated by an asterisk * or an asterisk followed by one or more decimal digits and a $ instead of a digit string. In this case, an int argument supplies the field width or precision. A negative field width is treated as a left adjustment flag followed by a positive field width; a negative precision is treated as though it were missing. If a single format directive mixes positional (nn$) and non-positional arguments, the results are undefined. The conversion specifiers and their meanings are: diouxX The int (or appropriate variant) argument is converted to signed decimal ( d and i), unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal ( x and X) notation. The letters "abcdef" are used for x conversions; the letters "ABCDEF" are used for X conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. DOU The long int argument is converted to signed decimal, unsigned octal, or unsigned decimal, as if the format had been ld, lo, or lu respectively. These conversion characters are deprecated, and will eventually disappear. eE The double argument is rounded and converted in the style [-d . ddd e \*[Pm] dd] where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00. For a, A, e, E, f, F, g, and G conversions, positive and negative infinity are represented as inf and -inf respectively when using the lowercase conversion character, and INF and -INF respectively when using the uppercase conversion character. Similarly, NaN is represented as nan when using the lowercase conversion, and NAN when using the uppercase conversion. fF The double argument is rounded and converted to decimal notation in the style [-ddd . ddd], where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it. gG The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit. aA The double argument is converted to hexadecimal notation in the style [-0x h . hhhp[\*[Pm]d]], where the number of digits after the hexadecimal-point character is equal to the precision specification. If the precision is missing, it is taken as enough to exactly represent the floating-point number; if the precision is explicitly zero, no hexadecimal-point character appears. This is an exact conversion of the mantissa+exponent internal floating point representation; the [-0x h . hhh] portion represents exactly the mantissa; only denormalized mantissas have a zero value to the left of the hexadecimal point. The p is a literal character p; the exponent is preceded by a positive or negative sign and is represented in decimal, using only enough characters to represent the exponent. The A conversion uses the prefix "0X" (rather than "0x"), the letters "ABCDEF" (rather than "abcdef") to represent the hex digits, and the letter P (rather than p) to separate the mantissa and exponent. C Treated as c with the l (ell) modifier. c The int argument is converted to an unsigned char , then to a wchar_t as if by btowc, and the resulting character is written. If the l (ell) modifier is used, the wint_t argument is converted to a wchar_t and written. S Treated as s with the l (ell) modifier. s The char * argument is expected to be a pointer to an array of character type (pointer to a string) containing a multibyte sequence. Characters from the array are converted to wide characters and written up to (but not including) a terminating NUL character; if a precision is specified, no more than the number specified are written. If a precision is given, no null character need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating NUL character. If the l (ell) modifier is used, the wchar_t * argument is expected to be a pointer to an array of wide characters (pointer to a wide string). Each wide character in the string is written. Wide characters from the array are written up to (but not including) a terminating wide NUL character; if a precision is specified, no more than the number specified are written (including shift sequences). If a precision is given, no null character need be present; if the precision is not specified, or is greater than the number of characters in the string, the array must contain a terminating wide NUL character. p The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx). n The number of characters written so far is stored into the integer indicated by the int * (or variant) pointer argument. No argument is converted. % A % is written. No argument is converted. The complete conversion specification is %%.
The decimal point character is defined in the programs locale (category LC_NUMERIC).
In no case does a non-existent or small field width cause truncation of a numeric field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.
#include <stdio.h> #include <wchar.h> /* Illustrates how to use wprintf API */ int example_wprintf(void) { int input = 12345; int rval; /* prints the integers on the standard output */ rval = wprintf(L"%d", input); /* return the number of wide characters that were written */ return(rval); }Output
12345
#include <stdio.h> #include <wchar.h> /* Illustrates how to use fwprintf API */ int example_fwprintf(void) { FILE *fp = NULL; int retval; wchar_t *wcs = L"abc"; /* open the file for writing*/ fp = fopen("write.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* print the characters into the file */ retval = fwprintf(fp, L"%s", wcs); /* Close the file opened for writing */ fclose(fp); /* return the number of wide characters that were written */ return (retval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use swprintf API */ int example_swprintf(wchar_t *buf, int maxlen) { wchar_t *wcs = L"abcdef"; int rval; /* prints the wide char string into the buffer buf */ rval = swprintf(buf, maxlen, L"%s", wcs); /* return the number of wide characters that were written */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vwprintf API */ int example_vwprintf(va_list input) { int rval; /* prints the integers on the standard output */ rval = vwprintf(L"%d", input); /* return the number of wide characters that were written */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vfwprintf API */ int example_vfwprintf(va_list input) { FILE *fp = NULL; int retval; /* open the file for writing*/ fp = fopen("write.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* print the characters into the file */ retval = vfwprintf(fp, L"%s", input); /* Close the file opened for writing */ fclose(fp); /* return the number of wide characters that were written */ return (retval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vswprintf API */ int example_vswprintf(wchar_t *buf, int maxlen, va_list input) { int rval; /* prints the wide char string into the buffer buf */ rval = vswprintf(buf, maxlen, L"%s", input); /* return the number of wide characters that were written */ return(rval); }
Security considerations:
Refer to printf() .
Limitations:
Long double length modifiers are not supported. The maximum floating point precision is 15 digits.
See also: btowc() fputws() printf() putwc() setlocale() wcsrtombs() wscanf()
IMPORT_C int | fwscanf | ( | struct __sFILE * | , |
const wchar_t * | , | |||
... | ||||
) |
IMPORT_C wint_t | getwchar | ( | void | ) |
The mbrlen function inspects at most n bytes pointed to by s to determine the number of bytes needed to complete the next multibyte character.
The mbstate_t argument, ps, is used to keep track of the shift state. If it is NULL, mbrlen uses an internal, static mbstate_t object, which is initialized to the initial conversion state at program startup.
It is equivalent to:
mbrtowc(NULL, s, n, ps);
Except that when ps is a NULL pointer, mbrlen uses its own static, internal mbstate_t object to keep track of the shift state. The behavior of the mbrlen is affected by LC_CTYPE category of the current locale.
size_t nchars(const char *s) { size_t charlen, chars; mbstate_t mbs; chars = 0; memset(&mbs;, 0, sizeof(mbs)); while ((charlen = mbrlen(s, MB_CUR_MAX, &mbs;)) != 0 && charlen != (size_t)-1 && charlen != (size_t)-2) { s += charlen; chars++; } return (chars); }
Errors:
The mbrlen function will fail if: [EILSEQ] An invalid multibyte sequence was detected. [EINVAL] The conversion state is invalid.
Limitations:
The current implementation of mbrlen is not affected by the LC_CTYPE category of the current locale. It works only for UTF8 character set.
The mbrtowc function inspects at most n bytes pointed to by s to determine the number of bytes needed to complete the next multibyte character. If a character can be completed, and pwc is not NULL, the wide character which is represented by s is stored in the wchar_t it points to.
If s is NULL, mbrtowc behaves as if pwc was NULL, s was an empty string ("") and n was 1.
The mbstate_t argument, ps, is used to keep track of the shift state. If it is NULL, mbrtowc uses an internal, static mbstate_t object, which is initialized to the initial conversion state at program startup.
The behavior of the mbrtowc is affected by LC_CTYPE category of the current locale.
#include <stdio.h> #include <stdlib.h> #include <wchar.h> /* Illustrates how to use mbrtowc API */ size_t example_mbrtowc() { size_t len; wchar_t wc[100]; char *s = 'a'; size_t n = 1; mbstate_t ps; /* converting multibyte sequence to a wide-char sequence */ len = mbrtowc(wc,(const char *) s, n, &ps;); /* checking for error value */ if(len < 0) { wprintf(L"mbrtowc returned error!! "); } /* returning no of bytes consumed */ return (len); }
Errors:
The mbrtowc function will fail if: [EILSEQ] An invalid multibyte sequence was detected. [EINVAL]The conversion state is invalid.
Limitations:
The current implementation of mbrtowc is not affected by the LC_CTYPE category of the current locale. It works only for UTF8 character set.
See also: mbtowc() setlocale() wcrtomb()
IMPORT_C int | mbsinit | ( | const mbstate_t * | ) |
The mbsinit function determines whether the mbstate_t object pointed to by ps describes an initial conversion state.
The behavior of the mbsinit is affected by LC_CTYPE category of the current locale.
#include <stdlib.h> #include <wchar.h> /* Illustrates how to use mbsinit API */ int example_mbsinit(void) { mbstate_t mbs; int state; /* testing for the initial state */ state = mbsinit(&mbs;); /* return the state */ return(state); }
Limitations:
The current implementation of mbsinit is not affected by the LC_CTYPE category of the current locale. It works only for UTF8 character set.
See also: mbrlen() mbrtowc() mbsrtowcs() wcrtomb() wcsrtombs()
The mbsrtowcs function converts a sequence of multibyte characters pointed to indirectly by src into a sequence of corresponding wide characters and stores at most len of them in the wchar_t array pointed to by dst, until it encounters a terminating null character ('\0'.)
If dst is NULL no characters are stored.
If dst is not NULL, the pointer pointed to by src is updated to point to the character after the one that conversion stopped at. If conversion stops because a null character is encountered, *src is set to NULL.
The mbstate_t argument, ps, is used to keep track of the shift state. If it is NULL, mbsrtowcs uses an internal, static mbstate_t object, which is initialized to the initial conversion state at program startup.
The mbsnrtowcs function behaves identically to mbsrtowcs, except that conversion stops after reading at most nms bytes from the buffer pointed to by src.
The behavior of the mbsrtowcs and mbsnrtowcs is affected by LC_CTYPE category of the current locale.
/* Illustrates how to use mbsrtowcs API */ #include <stdio.h> #include <stdlib.h> #include <wchar.h> size_t example_mbsrtowcs(wchar_t *wc, char *s, size_t n) { size_t len; mbstate_t mbs; /* converting multibyte string to a wide-char string */ len = mbsrtowcs(wc, &s;, n, &mbs;); /* checking for error */ if(len < 0) { wprintf(L"mbsrtowcs returned error!! "); } /* returning no of bytes consumed */ return (len); }
/* Illustrates how to use mbsrntowcs API */ #include <stdio.h> #include <stdlib.h> #include <wchar.h> size_t example_mbsnrtowcs(wchar_t *wc, char *s, size_t n, size_t nms) { size_t len; mbstate_t mbs; /* converting multibyte string to a wide-char string */ len = mbsnrtowcs(wc, &s;, nms, n, &mbs;); /* checking for error */ if(len < 0) { wprintf(L"mbsnrtowcs returned error!! "); } /* returning no of bytes consumed */ return (len); }
Errors:
The mbsrtowcs and mbsnrtowcs functions will fail if: [EILSEQ]An invalid multibyte character sequence was encountered. [EINVAL] The conversion state is invalid.
Limitations:
The current implementation of mbsrtowcs and mbsnrtowcs is not affected by the LC_CTYPE category of the current locale. It works only for UTF8 character set.
See also: mbrtowc() mbstowcs() wcsrtombs()
Parameters | |
---|---|
Note: This description also covers the following functions - mbsnrtowcs() |
IMPORT_C int | swprintf | ( | wchar_t * | , |
size_t | n, | |||
const wchar_t * | , | |||
... | ||||
) |
See also: btowc() fputws() printf() putwc() setlocale() wcsrtombs() wscanf()
IMPORT_C int | swscanf | ( | const wchar_t * | , |
const wchar_t * | , | |||
... | ||||
) |
The ungetwc function pushes the wide character wc (converted to an wchar_t ) back onto the input stream pointed to by stream . The pushed-backed wide characters will be returned by subsequent reads on the stream (in reverse order). A successful intervening call, using the same stream, to one of the file positioning functions fseek , fsetpos , or rewind will discard the pushed back wide characters.
One wide character of push-back is guaranteed, but as long as there is sufficient memory, an effectively infinite amount of pushback is allowed.
If a character is successfully pushed-back, the end-of-file indicator for the stream is cleared.
#include <stdio.h> #include <wchar.h> /* Illustrates how to use ungetwc API */ wint_t example_ungetwc(void) { FILE *fp = NULL; wint_t wc1; wint_t rval; wint_t wc2; /* opening the file to write*/ fp = fopen("input.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* character to written back to the fp */ wc = (wint_t) L'e'; /* write the character into the fp */ rval = ungetwc(wc, fp); /* check for error */ if(rval == WEOF) { wprintf(L"ungetwc returned error!! "); } /* Read the character from fp */ /* this char should be the same as the char */ /* that was written by ungetwc */ wc2 = getwc(fp); /* Close the file opened for writing */ fclose(fp); /* return the value that was written */ return (rval); }
IMPORT_C int | vfwprintf | ( | struct __sFILE * | , |
const wchar_t * | , | |||
va_list | ||||
) |
See also: btowc() fputws() printf() putwc() setlocale() wcsrtombs() wscanf()
Parameters | |
---|---|
Refer to fwprintf() for the documentation |
IMPORT_C int | vswprintf | ( | wchar_t * | , |
size_t | n, | |||
const wchar_t * | , | |||
va_list | ||||
) |
See also: btowc() fputws() printf() putwc() setlocale() wcsrtombs() wscanf()
Parameters | |
---|---|
Refer to fwprintf() for the documentation |
IMPORT_C int | vwprintf | ( | const wchar_t * | , |
va_list | ||||
) |
See also: btowc() fputws() printf() putwc() setlocale() wcsrtombs() wscanf()
Parameters | |
---|---|
Refer to fwprintf() for the documentation |
The wcrtomb function stores a multibyte sequence representing the wide character wc , including any necessary shift sequences, to the character array s , storing a maximum of MB_CUR_MAX bytes.
If s is NULL , wcrtomb behaves as if s pointed to an internal buffer and wc was a null wide character (L'\0').
The mbstate_t argument, ps , is used to keep track of the shift state. If it is NULL , wcrtomb uses an internal, static mbstate_t object, which is initialized to the initial conversion state at program startup.
The behavior of the wcrtomb is affected by LC_CTYPE category of the current locale.
#include <stdio.h> #include <stdlib.h> #include <wchar.h> /* Illustrates how to use wcrtomb API */ int example_wcrtomb(wchar_t wc) { char s[MAX_CUR_MAX]; size_t len; mbstate_t mbs; /* represent a wide-char in a single byte*/ len = wcrtomb(s, wc, &mbs;); /* return the number of bytes */ return(len); }
Errors:
The wcrtomb function will fail if: [EILSEQ] An invalid wide character code was specified. [EINVAL] The conversion state is invalid.
Limitations:
The current implementation of wcrtomb is not affected by the LC_CTYPE category of the current locale. It works only for UTF8 character set.
See also: mbrtowc() setlocale() wctomb()
IMPORT_C int | wcscoll | ( | const wchar_t * | , |
const wchar_t * | ||||
) |
The wcscoll function compares the null-terminated strings ws1 and ws2 according to the current locale collation order. In the "C" locale, wcscoll is equivalent to wcscmp .
#include <wchar.h> /* Illustrates how to use wcscoll API */ int example_wcscoll (void) { /* compares the two strings */ if( wcscoll(L"abcdef",L"abcdeg") != L'f'-L'g') return -1; return 0; }
Errors:
The wcscoll function will fail if: [EILSEQ] An invalid wide character code was specified. [ENOMEM] Cannot allocate enough memory for temporary buffers.
Limitations:
The current implementation of wcscoll is not affected by the LC_CTYPE category of the current locale. It is equivalent to wcscmp in this implementation.
See also: setlocale() strcoll() wcscmp() wcsxfrm()
Bugs:
The current implementation of wcscoll only works in single-byte LC_CTYPE locales, and falls back to using wcscmp in locales with extended character sets.
The wcsftime function is equivalent to the strftime function except for the types of its arguments. Refer to strftime() for a detailed description.
#include <wchar.h> #include <time.h> /* Illustatrates how to use wcsftime API */ int example_wcsftime() { wchar_t datestring[50]; int retval; struct tm *tm; /* get the current time */ time_t t = time(NULL); /* get the local time from the current time */ tm = localtime(&t;); /* convert date and time to a wide-character string */ retval = wcsftime(datestring,15,L"%A",tm); /* If the total number of resulting wide-character codes */ /* including the terminating null wide-character code is */ /* no more than maxsize, wcsftime() shall return the number */ /* of wide-character codes placed into the array pointed to */ /* by wcs, not including the terminating null wide-character */ /* code. Otherwise, zero is returned and the contents of the */ /* array are unspecified.*/ return retval; }
The wcsrtombs function converts a string of wide characters indirectly pointed to by src to a corresponding multibyte character string stored in the array pointed to by dst . No more than len bytes are written to dst .
If dst is NULL , no characters are stored.
If dst is not NULL , the pointer pointed to by src is updated to point to the character after the one that conversion stopped at. If conversion stops because a null character is encountered, *src is set to NULL .
The mbstate_t argument, ps , is used to keep track of the shift state. If it is NULL , wcsrtombs uses an internal, static mbstate_t object, which is initialized to the initial conversion state at program startup.
The wcsnrtombs function behaves identically to wcsrtombs , except that conversion stops after reading at most nwc characters from the buffer pointed to by src . The behavior of the wcsrtombs and wcsrntombs is affected by LC_CTYPE category of the current locale.
#include <stdlib.h> #include <wchar.h> /* Illustrates how to use wcsrtombs API */ size_t example_wcsrtombs(wchar_t *wcs, char *s, size_t n) { size_t len; mstate_t mbs; /* converting multibyte string to a wide-char string */ len = wcsrtombs(s, &wcs;, n, &mbs;); /* returning no of bytes that make up the multibyte sequence */ return (len;); }
#include <stdlib.h> #include <wchar.h> /* Illustrates how to use wcsnrtombs API */ size_t example_wcsnrtombs(wchar_t *wcs, char *s, size_t n, szie_t nmwc) { size_t len; mstate_t mbs; /* converting multibyte string to a wide-char string */ len = wcsrtombs(s, &wcs;, nwc, n, &mbs;); /* returning no of bytes that make up the multibyte sequence */ return (len;); }
Limitations:
The current implementation of wcsrtombs and wcsnrtombs is not affected by the LC_CTYPE category of the current locale. It works only for UTF8 character set.
See also: mbsrtowcs() wcrtomb() wcstombs()
Parameters | |
---|---|
Note: This description also covers the following functions - wcsnrtombs() |
The wcsxfrm function transforms a null-terminated wide character string pointed to by src according to the current locale collation order then copies the transformed string into dest . No more than len wide characters are copied into dest , including the terminating null character added. If len is set to 0 (it helps to determine an actual size needed for transformation), dest is permitted to be a NULL pointer.
Comparing two strings using wcscmp after wcsxfrm is equivalent to comparing two original strings with wcscoll .
#include <stdlib.h> #include <wchar.h> /* Illustrates how to use wcpcpy API */ int example_wcpcpy() { /* input string for which length to be found */ wchar_t *src = L"testcase"; wchar_t *dest = NULL; int length; /* allocate memory for the destination string */ dest = (wchar_t *)malloc((wcslen(src)+1)*sizeof(wchar_t)); /* perform the operation */ if(dest != NULL) length = wcsxfrm(dest,src,9); else { wprintf(L"ERROR : Cannot allocate memory"); return -1; } return length; }
Limitations:
The current implementation of wcsxfrm works only for "C" locale.
See also: setlocale() strxfrm() wcscmp() wcscoll()
Bugs:
The current implementation of wcsxfrm only works in single-byte LC_CTYPE locales, and falls back to using wcsncpy in locales with extended character sets. Comparing two strings using wcscmp after wcsxfrm is not always equivalent to comparison with wcscoll ; wcsxfrm only stores information about primary collation weights into dst , whereas wcscoll compares characters using both primary and secondary weights.
IMPORT_C wchar_t * | wcstok | ( | wchar_t * | , |
const wchar_t * | , | |||
wchar_t ** | ||||
) |
The wcstok function is used to isolate sequential tokens in a null-terminated wide character string, s. These tokens are separated in the string by at least one of the characters in delim . The first time that wcstok is called, s should be specified; subsequent calls, wishing to obtain further tokens from the same string, should pass a null pointer instead. The separator string, delim , must be supplied each time, and may change between calls. The context pointer last must be provided on each call.
The wcstok function is the wide character counterpart of the strtok_r function.
Examples:
#include <wchar.h> /* Illustrates how to use wcstok API */ int example_wcstok() { /* source wide character string */ const wchar_t *seps = L"onetwhr"; wchar_t *last, *tok, text[] = L"onetwothree"; tok = wcstok(text, seps, &last;); if(tok == NULL) return 0; else return 1; }
Examples:
The following code fragment splits a wide character string on ASCII space, tab and newline characters and writes the tokens to standard output:
const wchar_t *seps = L" "; wchar_t *last, *tok, text[] = L" one two three "; for (tok = wcstok(text, seps, &last;); tok != NULL; tok = wcstok(NULL, seps, &last;)) wprintf(L"%ls ", tok);
See also: strtok() wcschr() wcscspn() wcspbrk() wcsrchr() wcsspn()
Some early implementations of wcstok omit the context pointer argument, last, and maintain state across calls in a static variable like strtok does.
IMPORT_C long | wcstol | ( | const wchar_t * | , |
wchar_t ** | , | |||
int | ||||
) |
The wcstol , wcstoul , wcstoll , wcstoull , wcstoimax and wcstoumax functions are wide-character versions of the strtol , strtoul , strtoll , strtoull , strtoimax and strtoumax functions, respectively. Refer to their manual pages (for example strtol ) for details. The wcstoq and wcstouq are respectively equivalent to wcstoll and wcstoull.
#include <wchar.h> /* Illustrates how to use wcstoumax API */ uintmax_t example_wcstoumax() { /* src string */ wchar_t *src = L"478"; uintmax_t longVal; /* convert the wide character string to uintmax_t */ longVal = wcstoumax(src,NULL,10); /* return the converted long value */ return longVal; }
#include <wchar.h> /* Illustrates how to use wcstoimax API */ intmax_t example_wcstoimax() { /* src string */ wchar_t *src = L"478"; intmax_t longVal; /* convert the wide character string to intmax_t */ longVal = wcstoimax(src,NULL,10); /* return the converted long value */ return longVal; }
#include <wchar.h> /* Illustrates how to use wcstol API */ long example_wcstol() { /* src string */ wchar_t *src = L"478"; long longVal; /* call the API to convert src string to long value */ longVal = wcstol(src,NULL,10); /* return the converted long value */ return longVal; }
#include <wchar.h> /* Illustrates how to use wcstoul API */ unsigned long example_wcstoul() { /* src string */ wchar_t *src = L"478"; unsigned long longVal; /* call the API to convert src string to unsigned */ /* long value */ longVal = wcstoul(src,NULL,10); /* return the converted long value */ return longVal; }
#include <wchar.h> /* Illustatrates how to use wcstoll API */ long long example_wcstoll() { /* input string for which length to be found */ wchar_t *src = L"478"; long long longVal; /* perform the conversion operation from wide */ /* char string to long long value */ longVal = wcstoll(src,NULL,10); return longVal; }
#include <wchar.h> /* Illustatrates how to use wcstoull API */ unsigned long long example_wcstoull() { /* input string for which length to be found */ wchar_t *src = L"478"; unsigned long long longVal; /* perform the conversion operation from wide */ /*char string to unsigned long long value */ longVal = wcstoull(src,NULL,10); return longVal; }
#include <wchar.h> /* Illustrates how to use wcstoq API */ long long int example_wcstoq() { /* src string that contains decimal digits */ wchar_t *src = L"478"; long long int longVal; /* convert the decimal wide char string to long long int value */ longVal = wcstoq(src,NULL,10); /* return longVal and its value should be 478 */ return longVal; }
#include <wchar.h> /* Illustrates how to use wcstouq API */ unsigned long long int example_wcstouq() { /* src string that contains decimal digits */ wchar_t *src = L"478"; unsigned long long int longVal; /* convert the decimal wide char string to unsigned long long int value */ longVal = wcstouq(src,NULL,10); /* return longVal, which should be 478 */ return longVal; }
Parameters | |
---|---|
Note: This description also covers the following functions - wcstoul() wcstoll() wcstoull() wcstoq() wcstouq() wcstoimax() wcstoumax() |
IMPORT_C wchar_t * | wmemchr | ( | const wchar_t * | , |
wchar_t | , | |||
size_t | ||||
) |
The function wcslcat() appends a copy of the wide-character string pointed to by s2 (including the terminating null wide-character code) to the end of the wide-character string pointed to by s1. The initial wide-character code of string pointed to by s2 overwrites the null wide-character code at the end of s1. wcslcat() concatenates at most n-1 characters.
The function wcslcpy() copies the wide-character string pointed to by s2 (including the terminating null wide-character code) into the array pointed to by s1. It will copy at most n-1 characters.
The functions implement string manipulation operations over wide character strings. For a detailed description, refer to documents for the respective single-byte counterpart, such as memchr .
#include <wchar.h> /* Illustrates how to use wmemcmp API */ int example_wmemcmp() { /* source wide character string */ wchar_t *ws1 = L"test case",*ws2 = L"test case"; int retval; /* comparing the 2 wide-char strings */ retval = wmemcmp(ws1,ws2,500); /* checking for the return value */ if(retval != 0) return 1; else return 0; }
#include <wchar.h> /* Illustrates how to use wmemcpy API */ int example_wmemcpy() { /* source wide character string */ wchar_t ws1[50]=L"abcdefghij",*retval,ws2[50]=L"test"; /* compare the strings */ retval = wmemcpy(ws1,ws2,6); for(int i=0;i<6;i++) { if(retval[i] != ws2[i] || ws1[i] != ws2[i]) return 1; } return 0; }
#include <wchar.h> /* Illustrates how to use wmemmove API */ int example_wmemmove() { /* source wide character string */ wchar_t ws1[50]=L"abcdefghij",*retval,ws2[5] = L"abc"; int i; /* move the contents of ws2 to ws1 */ retval = wmemmove(ws1,ws2,3); /* compare the contents */ for(i=0;i<3;i++) { if(ws1[i] != ws2[i]) return 1; } return 0; }
#include <wchar.h> /* Illustrates how to use wmemset API */ int example_wmemset() { /* source wide character string */ wchar_t ws1[50]=L"abcdefghij", *retval; int i; /* setting the wide-string ws1 */ retval = wmemset(ws1,L'a',7); /* comparing the contents */ for(i=0;i<7;i++) { if(ws1[i] != L'a') return 1; } return 0; }
#include <wchar.h> /* Illustrates how to use wmemchr API */ wchar_t *example_wmemchr(void) { wchar_t *wcs = L"lmnopqr"; size_t n; wchar_t wc = L'p'; wchar_t *res; /* number of wide characters to be searched */ n = wcslen(wcs); /* search for the occurence of wchar wc in wide-char string wcs */ res = wmemchr(wcs, wc, n); /* return the pointer */ return(res); }
#include <wchar.h> /* Illustrates how to use wcslen API */ size_t example_wcslen(void) { wchar_t *wcs = L"defqwert"; size_t len; /* compute the length of the wide-char string */ len = wcslen(wcs); /* return the length of the wide-char string */ return (len); }
#include <wchar.h> /* Illustrates how to use wcscat API */ void example_wcscat(void) { wchar_t wcs[100] = L"abc"; wchar_t *wcs1 = L"def"; wchar *res; /* concatenate the two strings */ res = wcscat(wcs, wcs1); /* print the string that resulted from concatenation */ wprintf(L"Output wide-char string is : %ls ",res); }Output
abcdef
#include <wchar.h> /* Illustrates how to use wcschr API */ int example_ wcschr () { /* source wide character string */ wchar_t search_char[15] = L"&&&&$%%%%%TTU"; wchar_t *wstr; int i ; for(i = 0 ; search_char[i]; i++) { wstr = wcschr(L"", search_char[i]); if(wstr != NULL) { return -1; } } return 0; }
int example_wcscmp() { /* source wide character string */ wchar_t *wcs1 = "string1"; wchar_t *wcs2 = "string2"; int i ; i = wcscmp(wcs1, wcs2); if( i == 0) return -1; i = wcscmp(wcs1, L"string1"); if(i != 0) return -1; return 0; }
#include <wchar.h> /* Illustrates how to use wcscpy API */ int example_wcscpy () { /* source wide character string */ wchar_t wcs1[15]; wchar_t *res; /* copy the wide-char string into wcs1*/ res = wcscpy(wcs1, L"Hello world"); if(wcscmp(res, L"Hello world") != 0) return -1; return 0; }
#include <wchar.h> /* Illustrates how to use wcslcpy API */ int example_wcslcpy () { /* source wide character string */ wchar_t src[25] = L"Source"; wchar_t dest[20]= L"Destination"; /* copy the wide-char string into dest*/ size_t t = wcslcpy(dest,src,4); if(wcscmp(dest,L"Sou")!=0) return -1; return 0; }
#include <wchar.h> /* Illustrates how to use wcslcat API */ int example_wcslcat () { /* source wide character string */ wchar_t src[25] = L"Source"; wchar_t dest[20]= L"Destination"; /* concate the wide-char string into dest*/ size_t t = wcslcat(dest,src,14); if(wcscmp(dest,"DestinationSo")!=0) return -1; return 0; }
#include <wchar.h> /* Illustrates how to use wcscspnAPI */ int example_wcscspn () { /* source wide character string */ wchar_t wcs1[30] = L"1234567890ABCDE!@#$$"; wchar_t *wcs2[20] = { L"abcdefghijkl", L":::?>_)+(|{>", L"~*(&IUIJJJ;", L"1234567", L":::?>", L"1", L"as1sd2ds3ds48f5fd" }; int i; for( i = 0; i < 3 ; i ++) { if( wcslen(wcs1) != wcscspn(wcs1,wcs2[i])) return -1; } return 0; }
#include <wchar.h> /* Illustrates how to use wcsncat API */ int example_wcsncat () { /* source wide character string */ wchar_t *res; wchar_t str[50]; wcscpy(str, L"Hello"); res = wcsncat(str,L" world",12); if(wcscmp(str, L"Hello world") != 0) return -1; else return 0; }
#include <wchar.h> /* Illustrates how to use wcsncmp API */ int example_wcsncmp() { /* source wide character string */ wchar_t *wcs1 = L"Hello world"; if(wcsncmp(wcs1,wcs1,wcslen(wcs1))) return -1; else return 0; }
#include <wchar.h> /* Illustrates how to use wcsncpy API */ int example_wcsncpy () { /* source wide character string */ wchar_t wcs1[15] = L"Hello world"; wchar_t *res; res = wcsncpy(wcs1,wcs1,wcslen(wcs1)); if(wcscmp(res,wcs1)) return -1; return 0; }
#include <wchar.h> /* Illustrates how to use wcspbrk API */ int example_wcspbrk () { /* source wide character string */ wchar_t *wcs = L"abcdefghijkl"; wchar_t *res1 = wcspbrk(wcs, L""); if(res1 != NULL) return -1; return 0; }
#include <wchar.h> /* Illustrates how to use wcsrchr API */ int example_wcsrchr () { /* source wide character string */ wchar_t search_char[15] = L"&&&&$%%%%%TTU"; wchar_t *wstr; int i ; for(i = 0 ; search_char[i]; i++) { wstr = wcsrchr(L"", search_char[i]); if(wstr != NULL) { return -1; } } return 0; }
#include <wchar.h> /* Illustrates how to use wcsspn API */ int example_wcsspn() { /* source wide character string */ wchar_t wcs1[30] = L"1234567890ABCDE!@#$$"; if(wcslen(wcs1) != wcsspn(wcs1,wcs1)) return -1; return 0; }
#include <wchar.h> /* Illustrates how to use wcsstr API */ int example_wcsstr() { /* source wide character string */ wchar_t *buf[20] = { L"fkhsdf%%38",L"rtti123123", }; wchar_t *str = wcsstr(buf[0],L"\0"); wchar_t *str1 = wcsstr(buf[1],L"\0"); if(wcscmp(str,buf[0])) return 1; if(wcscmp(str1,buf[1])) return 1 return 0; }
See also: memchr() memcmp() memcpy() memmove() memset() strcat() strchr() strcmp() strcpy() strcspn() strlen() strncat() strncmp() strncpy() strpbrk() strrchr() strspn() strstr()
Parameters | |
---|---|
Note: This description also covers the following functions - wmemcmp() wmemcpy() wmemmove() wmemset() wcscat() wcschr() wcscmp() wcscpy() wcscspn() wcslcat() wcslcpy() wcslen() wcsncat() wcsncmp() wcsncpy() wcspbrk() wcsrchr() wcsspn() wcsstr() |
IMPORT_C int | wprintf | ( | const wchar_t * | , |
... | ||||
) |
See also: btowc() fputws() printf() putwc() setlocale() wcsrtombs() wscanf()
IMPORT_C int | wscanf | ( | const wchar_t * | , |
... | ||||
) |
The wscanf family of functions scans input according to a format as described below. This format may contain conversion specifiers ; the results from such conversions, if any, are stored through the pointer arguments. The wscanf function reads input from the standard input stream stdin , fwscanf reads input from the stream pointer stream , and swscanf reads its input from the wide character string pointed to by str . The vfwscanf function is analogous to vfwprintf and reads input from the stream pointer stream using a variable argument list of pointers. The vwscanf function scans a variable argument list from the standard input and the vswscanf function scans it from a wide character string; these are analogous to the vwprintf and vswprintf functions respectively. Each successive pointer argument must correspond properly with each successive conversion specifier (but see the * conversion below). All conversions are introduced by the % (percent sign) character. The format string may also contain other characters. White space (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input conversion cannot be made (see below).
Conversions:
Following the % character introducing a conversion there may be a number of flag characters, as follows: * Suppresses assignment. The conversion that follows occurs as usual, but no pointer is used; the result of the conversion is simply discarded. hh Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a char (rather than int ) . h Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a short int (rather than int ) . l (ell) Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a long int (rather than int ) , that the conversion will be one of a, e, f, or g and the next pointer is a pointer to double (rather than float ) , or that the conversion will be one of c or s and the next pointer is a pointer to an array of wchar_t (rather than char ) . ll (ell ell) Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a long long int (rather than int ) . L Indicates that the conversion will be one of a, e, f, or g and the next pointer is a pointer to long double . j Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a intmax_t (rather than int ) . t Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a ptrdiff_t (rather than int ) . z Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a size_t (rather than int ) . q (deprecated.) Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a long long int (rather than int ) . In addition to these flags, there may be an optional maximum field width, expressed as a decimal integer, between the % and the conversion. If no width is given, a default of "infinity" is used (with one exception, below); otherwise at most this many characters are scanned in processing the conversion. Before conversion begins, most conversions skip white space; this white space is not counted against the field width. The following conversions are available: % Matches a literal %. That is, "%%" in the format string matches a single input % character. No conversion is done, and assignment does not occur. d Matches an optionally signed decimal integer; the next pointer must be a pointer to int . i Matches an optionally signed integer; the next pointer must be a pointer to int . The integer is read in base 16 if it begins with 0x or 0X, in base 8 if it begins with 0, and in base 10 otherwise. Only characters that correspond to the base are used. o Matches an octal integer; the next pointer must be a pointer to unsigned int . u Matches an optionally signed decimal integer; the next pointer must be a pointer to unsigned int . x, X Matches an optionally signed hexadecimal integer; the next pointer must be a pointer to unsigned int . a, A, e, E, f, F, g, G Matches a floating-point number in the style of wcstod. The next pointer must be a pointer to float (unless l or L is specified.) s Matches a sequence of non-white-space wide characters; the next pointer must be a pointer to char , and the array must be large enough to accept the multibyte representation of all the sequence and the terminating NUL character. The input string stops at white space or at the maximum field width, whichever occurs first. If an l qualifier is present, the next pointer must be a pointer to wchar_t , into which the input will be placed. S The same as ls. c Matches a sequence of width count wide characters (default 1); the next pointer must be a pointer to char , and there must be enough room for the multibyte representation of all the characters (no terminating NUL is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format. If an l qualifier is present, the next pointer must be a pointer to wchar_t , into which the input will be placed. C The same as lc. [ Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char , and there must be enough room for the multibyte representation of all the characters in the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex ^. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. To include a hyphen in the set, make it the last character before the final close bracket; some implementations of wscanf use "A-Z" to represent the range of characters between A and Z. The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out. If an l qualifier is present, the next pointer must be a pointer to wchar_t , into which the input will be placed. p Matches a pointer value (as printed by %p in wprintf); the next pointer must be a pointer to void . n Nothing is expected; instead, the number of characters consumed thus far from the input is stored through the next pointer, which must be a pointer to int . This is not a conversion, although it can be suppressed with the * flag. The decimal point character is defined in the programs locale (category LC_NUMERIC). For backwards compatibility, a "conversion" of %\0 causes an immediate return of EOF.
#include <stdio.h> #include <wchar.h> /* Illustrates how to use wscanf API */ int example_wscanf(void) { int rval; int input; /* Display a message to the user to input an integer */ wprintf(L"Enter an integer : "); /* Read an integer from the standard input */ rval = wscanf(L"%d", &input;); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use swscanf API */ int example_swscanf(void) { int rval; wchar_t wcs[100]; wchar_t *bufptr = L"abcdefg"; /* Read a string from the buffer bufptr*/ rval = swscanf(bufptr, L"%s", wcs); /* The string that was read into wcs is printed */ wprintf(L"%s",wcs); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use fwscanf API */ int example_fwscanf(void) { FILE *fp = NULL; int retval; wchar_t wcs[100]; /* open the file for writing*/ fp = fopen("write.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* Read a string from fp*/ rval = fwscanf(fp, L"%s", wcs); /* Close the file that was opened */ fclose(fp); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vwscanf API */ int example_vwscanf(va_list arg) { int retval; /* Read a string from standard input */ rval = vwscanf(L"%s", arg); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vswscanf API */ int example_vswscanf(va_list arg) { int retval; wchar_t bufptr = L"abc"; /* Read a string from bufptr */ rval = vswscanf(bufptr, L"%s", arg); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vfwscanf API */ int example_vfwscanf(va_list arg) { FILE *fp = NULL; int retval; /* open the file for writing*/ fp = fopen("write.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* Read a string */ rval = vfwscanf(fp, L"%s", arg); /* Close the file that was opened */ fclose(fp); /* return the number of inputs that were read */ return(rval); }Examples:
#include <stdio.h> #include <wchar.h> /* Illustrates how to use wscanf API */ int example_wscanf(void) { int rval; int input; /* Display a message to the user to input an integer */ wprintf(L"Enter an integer : "); /* Read an integer from the standard input */ rval = wscanf(L"%d", &input;); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use swscanf API */ int example_swscanf(void) { int rval; wchar_t wcs[100]; wchar_t *bufptr = L"abcdefg"; /* Read a string from the buffer bufptr*/ rval = swscanf(bufptr, L"%s", wcs); /* The string that was read into wcs is printed */ wprintf(L"%s",wcs); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use fwscanf API */ int example_fwscanf(void) { FILE *fp = NULL; int retval; wchar_t wcs[100]; /* open the file for writing*/ fp = fopen("write.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* Read a string from fp*/ rval = fwscanf(fp, L"%s", wcs); /* Close the file that was opened */ fclose(fp); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vwscanf API */ int example_vwscanf(va_list arg) { int retval; /* Read a string from standard input */ rval = vwscanf(L"%s", arg); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vswscanf API */ int example_vswscanf(va_list arg) { int retval; wchar_t bufptr = L"abc"; /* Read a string from bufptr */ rval = vswscanf(bufptr, L"%s", arg); /* return the number of inputs that were read */ return(rval); }
#include <stdio.h> #include <wchar.h> /* Illustrates how to use vfwscanf API */ int example_vfwscanf(va_list arg) { FILE *fp = NULL; int retval; /* open the file for writing*/ fp = fopen("write.txt","w"); if(fp == NULL) { wprintf(L"Error: File open "); return (-1); } /* Read a string */ rval = vfwscanf(fp, L"%s", arg); /* Close the file that was opened */ fclose(fp); /* return the number of inputs that were read */ return(rval); }
Limitations:
Long double length modifiers are not supported.
See also: fgetwc() scanf() wcrtomb() wcstod() wcstol() wcstoul() wprintf()
Bugs:
In addition to the bugs documented in scanf , wscanf does not support the "A-Z" notation for specifying character ranges with the character class conversion (' %[ ').
IMPORT_C wchar_t * | wcswcs | ( | const wchar_t * | s, |
const wchar_t * | find | |||
) |
#include <wchar.h> /* Illustrates how to use wcswcs API */ int example_wcswcs() { /* source wide character string */ wchar_t *buf[20] = { L"fkhsdf%%38",L"rtti123123", }; wchar_t *str = wcswcs(buf[0],L"\0"); wchar_t *str1 = wcswcs(buf[1],L"\0"); if(wcscmp(str,buf[0])) return 1; if(wcscmp(str1,buf[1])) return 1 return 0; }
See also: strstr()
Parameters | |
---|---|
find | The wcswcs() function locates the first occurrence of the wide-character string pointed by s (excluding the terminating '\0' character) in the wide-character string pointed by find. |
IMPORT_C int | wpopen3 | ( | const wchar_t * | file, |
const wchar_t * | cmd, | |||
wchar_t ** | env, | |||
int | fids | |||
) |
Examples:
/* Illustrates how to use wpopen3 API */ #include <wchar.h> int main() { int fds[3]; int childid= wpopen3( NULL,NULL, NULL, fds); if (childid == -1 && errno == ENOENT) { printf("wpopen success"); return 0; } return -1; }
Output: wpopen success
Parameters | |
---|---|
fids | Note:wpopen3 is a wide-character version of popen3() |
IMPORT_C float | wcstof | ( | const wchar_t * | , |
wchar_t ** | ||||
) |
The wcstof , wcstod and wcstold functions are the wide-character versions of the strtof , strtod and strtold functions. Refer to strtod() for details.
#include <wchar.h> /* Illustrates how to use wcstof API */ int example_wcstof() { /* src string */ wchar_t wcs1[21] = L" 1.23abcd"; wchar_t wcs2[5]=L"abcd"; wchar_t *eptr; float d; /* convert wide-char string to float */ d = wcstof(wcs1, &eptr;); /* compare the result */ if((d == 1.23F) && !(wcscmp (eptr, wcs2))) return 0; else return 1; }
#include <wchar.h> /* Illustrates how to use wcstold API */ int example_wcstold() { /* src string */ wchar_t wcs1[21] = L" 1.23abcd"; wchar_t wcs2[5]=L"abcd"; wchar_t *eptr; double d; /* convert wide-char string to double */ d = wcstod(wcs1, &eptr;); /* compare the result */ if((d == 1.23) && !(wcscmp (eptr, wcs2))) return 0; else return 1; }
#include <wchar.h> /* Illustrates how to use wcstold API */ int example_wcstold() { /* src string */ wchar_t wcs1[21] = L" 1.23abcd"; wchar_t wcs2[5]=L"abcd"; wchar_t *eptr; long double d; /* convert wide-char string to long double */ d = wcstold(wcs1, &eptr;); /* compare the result * if((d == 1.23) && !(wcscmp (eptr, wcs2))) return 0; else return 1; }
Parameters | |
---|---|
Note: This description also covers the following functions - wcstold() wcstod() |
IMPORT_C int | wcswidth | ( | const wchar_t * | , |
size_t | ||||
) |
The wcswidth function determines the number of column positions required for the first n characters of pwcs , or until a null wide character (L'\0') is encountered.
The behavior of the wcswdith is affected by LC_CTYPE category of the current locale.
#include <wchar.h> /* Illustrates how to use wcswidth API */ int example_wcswidth() { /* wide character string for which width has to */ /* determined */ wchar_t *ws1 = L"test case"; int retval; /* compute the width of the ws1 */ retval = wcswidth(ws1,50); /* return the result */ return retval; }
Limitations:
The current implementation of wcswidth is dependent upon locale support in Symbian OS.
See also: iswprint() wcwidth()
IMPORT_C int | wcwidth | ( | wchar_t | ) |
The wcwidth function determines the number of column positions required to display the wide character wc .
The behavior of the wcwdith is affected by LC_CTYPE category of the current locale.
#include <wchar.h> /* Illustrates how to use wcwidth API */ int example_wcwidth() { /* wide character for which width has to be determined */ wchar_t wc = L'a'; int retval; /* determine the width of wc */ retval = wcwidth(wc); /* return the determined width */ return retval; }
Examples:
wint_t ch; int column, w; column = 0; while ((ch = getwchar()) != WEOF) { w = wcwidth(ch); if (w > 0 && column + w >= 20) { putwchar(L' \en'); column = 0; } putwchar(ch); if (ch == L' \en') column = 0; else if (w > 0) column += w; }
Limitations:
The current implementation of wcwidth is dependent upon the locale support of Symbian OS.
See also: iswprint() wcswidth()
See also: mbrtowc() mbstowcs() wcsrtombs()
Parameters | |
---|---|
Refer to mbsrtowcs() for the documentation |
See also: mbsrtowcs() wcrtomb() wcstombs()
Parameters | |
---|---|
Refer to wcsrtombs() for the documentation |
IMPORT_C wchar_t * | wcpcpy | ( | wchar_t * | dst, |
const wchar_t * | src | |||
) |
The wcpcpy() function copies the wide-character string src to dst (including the terminating '\0' character.)
#include <stdlib.h> #include <wchar.h> /* Illustrates how to use wcpcpy API */ int example_wcpcpy() { /* input string for which length to be found */ wchar_t *src = L"testcase"; wchar_t *dest = NULL; wchar_t *destEndPtr = NULL; /* allocate memory for the destination string */ dest = (wchar_t *)malloc((wcslen(src)+1)*sizeof(wchar_t)); /* perform the operation */ if(dest != NULL) destEndPtr = (wchar_t *)wcpcpy(dest,src); else return 1; if(!wcscmp(dest,src)) { free(dest); return 0; } else { free(dest); return 1; } }
IMPORT_C wchar_t * | wcpncpy | ( | wchar_t * | dst, |
const wchar_t * | src, | |||
size_t | n | |||
) |
The wcpncpy() function copies at most n wide characters from the wide-character string src , including the terminating L'\0' character, to dst . Exactly n wide characters are written at dst . If the length wcslen(src) is less than n , the remaining wide characters in the array dst are filled with L'\0' characters. If the length wcslen(src) is greater than or equal to n , the string dst will not be L'\0' terminated.
#include <stdlib.h> #include <wchar.h> /* Illustrates how to use wcpncpy API */ int example_wcpncpy(void) { /* input string for which length to be found */ wchar_t *src = L"testcase"; wchar_t *dest = NULL; wchar_t *destEndPtr = NULL; /* allocate memory for the destination string */ dest = (wchar_t *)malloc((wcslen(src)+1)*sizeof(wchar_t)); /* perform the operation */ if(dest != NULL) destEndPtr = (wchar_t *)wcpncpy(dest,src,wcslen(src)+1); else return -1; /* checking for error */ if(!wcscmp(dest,src)) { wprintf(L"wcpncpy succeeded "); free(dest); return 0; } else { wprintf(L"wcpncpy failed!! "); free(dest); return -1; } }
See also: wcpcpy()
IMPORT_C int | wcscasecmp | ( | const wchar_t * | s1, |
const wchar_t * | s2 | |||
) |
The wcscasecmp() function compares the null-terminated wide-character strings s1 and s2 .
#include <wchar.h> /* Illustrates how to use wcscasecmp API */ int example_wcscasecmp(void) { /* input strings which needs to be compared */ wchar_t *ws1=L"testcasecmp"; wchar_t *ws2=L"TESTCASECMP"; /* function call to compare the two strings which */ /* differ in case */ int retval = wcscasecmp(ws1,ws2); /* returns 0 if they are equal or > 1 */ /* if first string is greater than second string else -1 */ return retval; }
See also: strcasecmp()
IMPORT_C wchar_t * | wcsdup | ( | const wchar_t * | srcwcs | ) |
The wcsdup() function allocates sufficient memory for a copy of the wide-string srcwcs, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free .
If insufficient memory is available, NULL is returned and errno is set to ENOMEM.
#include <wchar.h> /* Illustrates how to use wcsdup API */ int example_wcsdup(void) { /* input string for which length has to be found */ wchar_t *srcws=L"testwcsdup"; wchar_t *destws = NULL; /* invoke the API wcsdup to duplicate the string */ destws = wcsdup(srcws); /* return no error if src str and dest str are equal else return an error */ if(!(wcscmp(srcws,destws))) { free(destws); return 1; } else { free(destws); return -1; } }
See also: strdup()
IMPORT_C int | wcsncasecmp | ( | const wchar_t * | s1, |
const wchar_t * | s2, | |||
size_t | n | |||
) |
The wcsncasecmp() function compares atmost n wide-characters from the null-terminated wide-character strings s1 and s2 .
#include <wchar.h> /* Illustrates how to use wcsncasecmp API */ int example_wcsncasecmp() { /* input strings which need to be compared */ wchar_t *ws1=L"testcasecmp"; wchar_t *ws2=L"TESTCASECMP"; /* function call to compare the two strings which */ /* differ in case */ int retval = wcsncasecmp(ws1,ws2,11); /* returns 0 if they are equal except the case or >1 */ /* if first string is greater than second string else -1 */ return retval; }
See also: strncasecmp()
#include <wchar.h> /* Illustrates how to use wcsnlen API */ size_t example_wcsnlen() { /* input string for which length to be found */ wchar_t *ws=L"testwcsnlen"; /* find the length of the wide char string by */ /* calling wcsnlen */ size_t retval = wcsnlen(ws,14); /* return the number of wide chars in the string */ /* if retval is less than 14 Else return 14 as the */ /* length of the string */ return retval; }
See also: strnlen()
Parameters | |
---|---|
maxlen | The wcsnlen() function computes the length of the wide-character string s not including the terminating NUL character. It looks at only first maxlen wide-characters in s and never beyond s+maxlen. |
IMPORT_C wchar_t * | wrealpath | ( | const wchar_t * | , |
wchar_t * | ||||
) |
The wrealpath function resolves all extra "/" characters and references to /./ and /../ in path, and copies the resulting absolute path into the memory referenced by resolved path. The resolved path argument must refer to a buffer capable of storing at least PATH_MAX characters. The wrealpath function will resolve both absolute and relative paths and return the absolute path corresponding to path. All but the last component of path must exist when wrealpath is called.
#include<stdlib.h> #include<stdio.h> //printf #include<sys/stat.h> //S_IWUSR #include<sys/syslimits.h> //PATH_MAX #include<unistd.h> //chdir #inlclude<wchar.h> int main() { wchar_t resolvepath[PATH_MAX]; FILE *fp = NULL; wchar_t *rpath = NULL; fp = wfopen(L"c:\xyz.txt", L"w"); if(!fp) { printf("wfopen failed!!"); return -1; } wmkdir(L"c:\tmdir", S_IWUSR); int c = wchdir(L"c:\"); if(c == -1) { printf("wchdir failed!!"); return -1; } rpath = wrealpath(L".\tmdir\..\xyz.txt", resolvepath); printf("resolvepath: L%s ", resolvepath); if(rpath != NULL) printf("rpath: L%s ", rpath); fclose(fp); wrmdir(L"c:\tmdir"); wunlink(L"c:\xyz.txt"); return 0; }Output
resolvepath: C:\xyz.txt rpath: C:\xyz.txt
IMPORT_C int | wrmdir | ( | const wchar_t * | ) |
The wrmdir system call removes a directory file whose name is given by _path. The directory must not have any entries other than '.' and '..'.
/* Detailed description: This test code demonstrates usage of wrmdir systemcall, it removes directory Example from the current working directory. Preconditions: Expects empty directoy "Example" in current working directory. */ #include <wchar.h> int main() { if(wrmdir(L"Example") < 0 ) { printf("wrmdir failed "); return -1; } printf("Directory Example removed "); return 0; } Output Directory Example removed
Capability | |
---|---|
Deferred |
IMPORT_C int | wstat | ( | const wchar_t * | name, |
struct stat * | st | |||
) |
The wstat system call obtains information about the file pointed to by name. Read, write or execute permission of the named file is not required, but all directories listed in the path name leading to the file must be searchable.
The sb argument is a pointer to a stat structure as defined by #include <sys/stat.h> and into which information is placed concerning the file.
The fields of struct stat related to the file system are as follows: st_dev The numeric ID of the device containing the file. st_ino The file's inode number. st_nlink The number of hard links to the file.
The st_dev and st_ino fields together identify the file uniquely within the system.
The time-related fields of struct stat are as follows: st_atime Time when file data last accessed. Changed by the .Xr utimes 2, read and readv system calls. st_mtime Time when file data last modified. st_ctime Time when file status was last changed (inode data modification). st_birthtime Time when the inode was created.
#ifndef _POSIX_SOURCE #define st_atime st_atimespec.tv_sec #define st_mtime st_mtimespec.tv_sec #define st_ctime st_ctimespec.tv_sec #endif
The size-related fields of the struct stat are as follows: st_size The file size in bytes. st_blksize The optimal I/O block size for the file. st_blocks The actual number of blocks allocated for the file in 512-byte units. As short symbolic links are stored in the inode, this number may be zero.
The access-related fields of struct stat are as follows: st_uid The user ID of the file's owner. st_gid The group ID of the file. st_mode Status of the file (see below).
#define S_IFMT 0170000 // type of file #define S_IFIFO 0010000 // named pipe (fifo) #define S_IFCHR 0020000 // character special #define S_IFDIR 0040000 // directory #define S_IFBLK 0060000 // block special #define S_IFREG 0100000 // regular #define S_IFLNK 0120000 // symbolic link #define S_IFSOCK 0140000 // socket #define S_IFWHT 0160000 // whiteout #define S_ISUID 0004000 // set user id on execution #define S_ISGID 0002000 // set group id on execution #define S_ISVTX 0001000 // save swapped text even after use #define S_IRUSR 0000400 // read permission, owner #define S_IWUSR 0000200 // write permission, owner #define S_IXUSR 0000100 // execute/search permission, owner
For a list of access modes, see #include <sys/stat.h> The following macros are available to test whether a st_mode value passed in the m argument corresponds to a file of the specified type: S_ISBLK (m); Test for a block special file. S_ISCHR (m); Test for a character special file. S_ISDIR (m); Test for a directory. S_ISFIFO (m); Test for a pipe or FIFO special file. S_ISLNK (m); Test for a symbolic link. NOTE: Inode structure is not supported by Symbian OS and hence link count updation is not possible. Check for symbolic link would always fail because of this reason. S_ISREG (m); Test for a regular file. S_ISSOCK (m); Test for a socket. S_ISWHT (m); Test for a whiteout.
The macros evaluate to a non-zero value if the test is true or to the value 0 if the test is false.
st_dev The numeric ID of the device containing the file. st_ino The file's inode number. st_nlink The number of hard links to the file.
st_atime Time when file data last accessed. Changed by the .Xr utimes 2, read and readv system calls. st_mtime Time when file data last modified. st_ctime Time when file status was last changed (inode data modification). st_birthtime Time when the inode was created.
st_size The file size in bytes. st_blksize The optimal I/O block size for the file. st_blocks The actual number of blocks allocated for the file in 512-byte units. As short symbolic links are stored in the inode, this number may be zero.
st_uid The user ID of the file's owner. st_gid The group ID of the file. st_mode Status of the file (see below).
Test for a block special file. Test for a character special file. Test for a directory. Test for a pipe or FIFO special file. Test for a symbolic link. NOTE: Inode structure is not supported by Symbian OS and hence link count updation is not possible. Check for symbolic link would always fail because of this reason. Test for a regular file. Test for a socket. Test for a whiteout.
/* Detailed description: Sample usage of wstat system call Preconditions: Example.txt file should be present in working directory */ #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <wchar.h> int main() { struct stat buf; if(wstat(L"Example.txt" , &buf;) < 0 ) { printf("Failed to wstat Example.txt "); return -1; } printf("wstat system call succeded "); return 0; }Output
wstat system call succeded
Capability | |
---|---|
Deferred | RFs::Entry(const TDesC16&, TEntry&) |
IMPORT_C int | wsystem | ( | const wchar_t * | ) |
The wsystem function spawns another process with the argument cmd. The calling process waits for the child process to finish executing.
If cmd is a NULL pointer, wsystem will return non-zero to indicate that wsystem is suporrted and zero if it is not supported.
#include <stdlib.h> #include <stdio.h> //printf #include <wchar.h> int main( void ) { int retVal = -1; printf( "Calling wsystem()... " ); /* helloworld.exe is an executable that just prints "Hello world!" and it should be created before executing this example code. */ retVal = wsystem(L"c:\sys\bin\helloworld.exe"); /* Print the return value of wsystem() */ printf( "wsystem() returned: %d", retVal ); return 0; }Output
Calling wsystem()... wsystem() returned: -1
See also: wpopen()
IMPORT_C int | wunlink | ( | const wchar_t * | ) |
The wunlink system call removes the link named by _path from its file system.
Symbian OS simulates links so there is no reference count and files are unaware of links. Calling unlink on a file will always close the file, regardless of whether there is another link.
The _path argument may not be a directory.
/* Detailed description: Example to wunlink a link file Precondition: A file by name "Link" should exist in c: drive. */ #include <wchar.h> #include <stdio.h> int main(void) { if(wunlink(L"C:\Link")) { printf("wunlink on link file failed"); } printf("wunlink on link file succeeded"); }Output
wunlink on link file succeeded.
See also: close() rmdir()
Capability | |
---|---|
Deferred | RFs::Att(const TDesC16&, unsigned&) |
Deferred | RFs::SetAtt(const TDesC16&, unsigned, unsigned) |
Deferred |
IMPORT_C FILE * | wpopen | ( | const wchar_t * | command, |
const wchar_t * | mode | |||
) |
The wpopen function opens a process by creating a pipe and invoking the shell (creating a process). Since a pipe is by definition unidirectional, The wmode argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only ("r") or write-only "w". If type is anything other than this, then the behavior is undefined.
The command argument is a pointer to a null-terminated wide character string containing a shell command line. This command is passed to /bin/sh using the - c flag; interpretation, if any, is performed by the shell.
The return value from wpopen is a normal standard I/O stream in all respects save that it must be closed with pclose rather than fclose. Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that called wpopen, unless this is altered by the command itself. Conversely, reading from a "wpopened" stream reads the command's standard output, and the command's standard input is the same as that of the process that called wpopen.
Note that output wpopen streams are fully buffered by default.
See also: popen() pclose() pipe() fclose() fflush() fopen() system()
Bugs:
Since the standard input of a command opened for reading shares its seek offset with the process that called wpopen, if the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can be avoided by calling fflush before wpopen. Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The only hint is an exit status of 127. The wpopen function always calls sh, never calls csh.
IMPORT_C int | wopen | ( | const wchar_t * | , |
int | , | |||
... | ||||
) |
The wide character file-name specified by the wide character string file is opened for reading and/or writing as specified by the argument flags and the file descriptor returned to the calling process. The flags argument may indicate the file is to be created if it does not exist (by specifying the O_CREAT flag). In this case wopen requires a third argument mode_t mode , and the file is created with mode mode as described in chmod and modified by the process' umask value (see umask
The flags specified are formed by or 'ing the following values
O_RDONLYopen for reading only O_WRONLYopen for writing only O_RDWRopen for reading and writing O_APPENDappend on each write O_CREATcreate file if it does not exist O_TRUNCtruncate size to 0 O_EXCLerror if create and file exists
Opening a file with O_APPEND set causes each write on the file to be appended to the end. If O_TRUNC is specified and the file exists, the file is truncated to zero length. If O_EXCL is set with O_CREAT and the file already exists, wopen returns an error. This may be used to implement a simple exclusive access locking mechanism. If O_EXCL is set and the last component of the path is a symbolic link, wopen will fail even if the symbolic link points to a non-existent name.
If successful, wopen returns a non-negative integer, termed a file descriptor. It returns -1 on failure. The file pointer used to mark the current position within the file is set to the beginning of the file.
When a new file is created it is given the group of the directory which contains it.
The new descriptor is set to remain wopen across execve system calls; see close and fcntl .
The system imposes a limit on the number of file descriptors wopen simultaneously by one process. The getdtablesize system call returns the current system limit.
Notes:
1) Mode values for group and others are ignored 2) Execute bit and setuid on exec bit are ignored. 3) The default working directory of a process is initalized to C: \p rivate \U ID (UID of the calling application) and any data written into this directory persists between phone resets. 4) If the specified file is a symbolic link and the file it is pointing to is invalid, the symbolic link file will be automatically removed. 5) A file in cannot be created with write-only permissions. Attempting to create a file with write-only permissions will result in a file with read-write permission. 6) Creating a new file with the O_CREAT flag does not alter the time stamp of the parent directory. 7) A file has only two time stamps: access and modification. Creation time stamp of the file is not supported and access time stamp is initially set equal to modification time stamp.
/*This example creates a file in the current working directory and opens it in read-write mode. */ #include <sys/types.h> #include <sys/stat.h> #include <wchar.h> int main() { int fd = 0 ; fd = wopen(L"Example.txt" , O_CREAT | O_EXCL , 0666) ; if(fd < 0 ) { printf("Failed to create and wopen file in current working directory ") ; return -1 ; } printf("File created and opened in current working directory " ) ; return 0 ; }Output
File created and opened in current working directory
See also: chmod() close() dup() getdtablesize() lseek() read() umask() write() fopen() open()
Capability | |
---|---|
Deferred | RFs::Entry(const TDesC16&, TEntry&) |
IMPORT_C FILE * | wfopen | ( | const wchar_t * | , |
const wchar_t * | ||||
) |
"r" Open text file for reading. The stream is positioned at the beginning of the file. "r+" Open for reading and writing. The stream is positioned at the beginning of the file. "w" Truncate to zero length or create text file for writing. The stream is positioned at the beginning of the file. "w+" Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. "a" Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek or similar. "a+" Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek or similar.
The mode string can also include the letter "b" either as a third character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with -isoC and has no effect; the "b" is ignored.
Reads and writes may be intermixed on read/write streams in any order, and do not require an intermediate seek as in previous versions of stdio. This is not portable to other systems, however; ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file.
/* this program shows opening in write mode,write data and close */ /* again open in append mode and write data */ /* Check file c:\wfopen.txt */ #include <stdio.h> int main(void) { FILE *fp; wchar_t* name = L"c:\wfopen.txt"; if ((fp = wfopen (name, L"wb")) == NULL) { printf("Error creating file"); return -1; } printf("Opened file "); fprintf(fp, "This is the first line "); printf("Wrote to file "); fclose (fp); printf("Closed file "); if ((fp = wfopen (name, L"a")) == NULL) { printf("Error opening file"); return -1; } printf("Opened file for appending "); fprintf(fp, "This is the second line "); fclose (fp); printf("closed file, check output in c:\ wfopen.txt file "); wunlink(name); return 0; }Output
Opened file Wrote to file Closed file Opened file for appending closed file, check output in c:\ wfopen.txt file
Errors:
[EINVAL] The mode argument to wfopen, was invalid. The wfopen, functions may also fail and set errno for any of the errors specified for the routine malloc. The wfopen function may also fail and set errno for any of the errors specified for the routine wopen.
Note:
1) Mode values for group and others will be ignored for file creation,execute bit and setuid on exec bit on an file don't have any effect while file creation. Default working directory of a process is initialized to C:\private\UID (UID of the calling application) and any data written into this directory persists between phone resets. 2) If the specified file is a symbolic link and the file it is pointing to is invalid the symbolic link file will be automatically removed.
Limitations:
A file in cannot be created with write-only permission and attempting to create one will result in a file with read-write permission. Creating a new file with the O_CREAT flag does not alter the time stamp of its parent directory. The newly created entry has only two time stamps: access and modification. Creation time stamp is not supported and access time stamp is initially equal to modification time stamp. open, fclose and fflush.
Capability | |
---|---|
Deferred | RFs::Entry(const TDesC16&, TEntry&) |
IMPORT_C int | wrename | ( | const wchar_t * | oldpath, |
const wchar_t * | newpath | |||
) |
The wrename system call causes the link with the wide character name oldpath to be renamed with the wide character name newpath. If newpath exists, it is first removed. Both oldpath and newpath must be of the same type (that is, both directories or both non-directories), and must reside on the same file system.
If the final component of from is a symbolic link the symbolic link is renamed, not the file or directory to which it points.
If a file with a symbolic link pointing to it is renamed, then a subsequent open call on the symbolic link file would automatically remove the link file, i.e consider a symbolic file link.x pointing to a file abc.x. If the file abc.x is renamed to abcd.x then, a subsequent open call on link.x file would automatically remove link.x file.
Notes:
wrename fails if from and to are files and are in use (i.e. if any either of the files is open).
Parent directory time stamps are uneffected.
The new entry has only two time stamps: modification and access. The access time stamp is initially equal to modification time stamp.
/* Detailed description : This sample code demonstrates usage of wrename system call. Preconditions : Example.cfg file should be present in the current working directory. */ #include <wchar.h> #include <stdio.h> int main() { if(wrename(L"Example.txt" , L"Example2.txt") < 0 ) { printf("Failed to wrename Example.txt "); return -1; } printf("wrename successful "); return 0; }Output:
wrename successful
Errors:
The wrename system call will fail and neither of the argument files will be affected if: [EINVAL] Invalid argument. [ENAMETOOLONG] A component of a path exceeded 255 characters. [ENOENT] The named file does not exist. [EACCES] Search permission is denied for a component of the path prefix. [EACCES] The from argument is a parent directory of to, or an attempt is made to wrename . or ...
Capability | |
---|---|
Deferred | |
Deferred | RFs::Entry(const TDesC16&, TEntry&) |
Deferred | |
Deferred | RFs::SetAtt(const TDesC16&, unsigned, unsigned) |
Deferred |
IMPORT_C int | wchdir | ( | const wchar_t * | _path | ) |
The _path argument points to the pathname of a directory which is a wide character string. The wchdir system call causes the named directory to become the current working directory, that is, the starting point for _path searches of pathnames not beginning with a slash, '/.'
#include<wchar.h> /* wmkdir, wrmdir */ #include <sys/stat.h> /* S_IRWXU */ #include <stdio.h> /* printf */ int main() { int ret_wmkdir = wmkdir(L"dirName", S_IRWXU); /* create directory */ if(ret_wmkdir < 0) { printf("error creating directory"); return -1; } else { int ret_wchdir = wchdir(L"dirName"); /* change directory */ if(ret_wchdir < 0) { printf("error changing directory"); return -1; } else { printf("working directory changed"); } wrmdir(L"dirname"); /* remove directory */ } return 0; }Output
working directory changed
See also: chdir()
Capability | |
---|---|
Deferred | RFs::Att(const TDesC16&, unsigned&) |
Deferred |
IMPORT_C int | wchmod | ( | const wchar_t * | _path, |
mode_t | _mode | |||
) |
The file permission bits of the wide character file-name specified by _path are changed to _mode. The wchmod system call verifies that the process owner (user) either owns the file specified by _path is the super-user. A mode is created from ord permission bit masks defined in #include <sys/stat.h>:
#define S_IRWXU 0000700 // RWX mask for owner #define S_IRUSR 0000400 // R for owner #define S_IWUSR 0000200 // W for owner #define S_IXUSR 0000100 // X for owner #define S_IRWXG 0000070 // RWX mask for group #define S_IRGRP 0000040 // R for group #define S_IWGRP 0000020 // W for group #define S_IXGRP 0000010 // X for group #define S_IRWXO 0000007 // RWX mask for other #define S_IROTH 0000004 // R for other #define S_IWOTH 0000002 // W for other #define S_IXOTH 0000001 // X for other #define S_ISUID 0004000 // set user id on execution #define S_ISGID 0002000 // set group id on execution #ifndef __BSD_VISIBLE #define S_ISTXT 0001000 // sticky bit #endif
Note : sticky bit and setuid on exec bit are not supported, permission values for users is considered while premissions values for group and others are ignored(As there is no concept of group and others). An attempt to change file permission to write-only changes the file permission to read-write. Permissions for directory will be ignored.
#include <wchar.h> /* wchmode, wfopen */ #include <sys/stat.h> /* S_IWUSR */ #include <stdio.h> /* printf */ int main() { FILE * wfp = wfopen(L"fileName.txt", L"wb"); /* create file */ if(wfp) { fclose(wfp); /* close file */ int ret = wchmod(L"fileName", S_IWUSR); /* change mode */ if(ret < 0) { printf("error changing mode"); return -1; } else { printf("mode changed"); } } else { printf("error creating file"); return -1; } return 0; }Output
mode changed
Capability | |
---|---|
Deferred | RFs::SetAtt(const TDesC16&, unsigned, unsigned) |
IMPORT_C wchar_t * | wgetcwd | ( | wchar_t * | _buf, |
size_t | _size | |||
) |
The wgetcwd function copies the absolute pathname of the current working directory into the memory referenced by _buf which is a wchar_t pointer and returns a pointer to _buf. The size argument is the _size, in bytes, of the array referenced by _buf.
If _buf is NULL, space is allocated as indicated by size to store the pathname and the current working directory is returned as long as the _size bytes are sufficient to hold the working directory name. This space may later be free'd.
This routine has traditionally been used by programs to save the name of a working directory for the purpose of returning to it. A much faster and less error-prone method of accomplishing this is to open the current directory ('.') and use the fchdir function to return.
#include<wchar.h> #include<stdio.h> #include <stdlib.h> #define BUF_SIZE 100 int main() { int c; long size = BUF_SIZE; wchar_t *buf = NULL; wchar_t *ptr = NULL; c = wchdir("c:\"); /* change directory to c: */ buf = (wchar_t *)malloc((size_t)size); if (buf != NULL && c == 0) { ptr = wgetcwd(buf, (size_t)size); /* call wgetcwd to fetch the current directory */ printf("wgetcwd returned: %s ", ptr); } free(buf); return 0; }Output
wgetcwd returned: c:\
Notes:
The wgetcwd function returns the default working directory as c:\private\XXXXXXXX (where XXXXXXXX is the UID of the process) as the default session path is initialised to c:\private\current_process'_UID in Symbian OS.
See also: chdir() malloc() strerror() wgetcwd()
IMPORT_C int | wmkdir | ( | const wchar_t * | _path, |
mode_t | _mode | |||
) |
The directory with the wide character name _path is created with the access permissions specified by _mode.
Notes:
mode is ignored.
Parent directory time stamps are not updated.
#include <sys/stat.h> #include <wchar.h> #include <stdio.h> int main() { if(wmkdir(L"Example" , 0666) < 0 ) { printf("Directory creation failed "); return -1; } printf("Directory Example created "); return 0; }Output
Directory Example created
Capability | |
---|---|
Deferred |
IMPORT_C int | wclosedir | ( | WDIR * | dp | ) |
The wclosedir function closes the named directory stream and frees the structure associated with the dirp pointer.
/* Detailed description: This test code demonstrates usage of wclose system call.* Preconditions: Expects Test directory to be present in current working directory. */ #include <wchar.h> #include <dirent.h> int main() { WDIR *DirHandle; if(!(DirHandle = wopendir(L"Test") ) ) { printf("Failed to open directoy Test "); return -1; } if(wclosedir(DirHandle) < 0 ) { printf("Close dir failed "); return -1; } printf("Directory Test closed "); return 0; }Output
Directory Test closed
See also: wopendir() wreaddir() opendir() readdir()
The wreaddir function returns a wdirent pointer to the next directory entry. It returns NULL upon reaching the end of the directory or detecting an invalid wreaddir operation on the directory stream. The new position reverts to the one associated with the directory stream when the wtelldir operation was performed.
Note: wreaddir operates on a static directory structure created by wopendir . Asynchronous operations on the directory itself will not be reflected in calls to wreaddir .
/* Detailed description: Sample usage of wreaddir call Preconditions: Example Directory should be present in current working directory and should contain atleast one file/directory. */ #include <dirent.h> #include <unistd.h> #include <wchar.h> int main() { WDIR *dirName; struct wdirent *Dir; dirName = wopendir(L"Example"); if(dirName == NULL ) { printf("Failed to open directory "); return -1; } if(!(Dir = wreaddir(dirName)) ) { printf("Read dir failed "); return -1; } printf("Directory Example read "); wclosedir(dirName); return 0; }Output
Directory Example read
See also: wopendir() wreaddir() wrewinddir() wtelldir() wseekdir() wclosedir() opendir() readdir() rewinddir() telldir() seekdir() closedir() close() lseek() open() read()
IMPORT_C void | wrewinddir | ( | WDIR * | dp | ) |
The wrewinddir function function resets the position of the named directory stream pointed by 'dirp' to the beginning of the directory.
/* Detailed description: Sample usage of wreaddir call Preconditions: Example Directory should be present in current working directory and should contain say 4 files/directories. */ #include <dirent.h> #include <unistd.h> #include <wchar.h> int main() { WDIR *dirName; off_t offset; struct wdirent *Dir; dirName = wopendir(L"Example"); if(dirName == NULL ) { printf("Failed to open directory "); return -1; } wseekdir(dirName, 3) if((offset = wtelldir(dirName))!= 3) { printf("failed "); return -1; } wrewindir(dirName); if((offset = wtelldir(dirName))!= 0) { printf("failed "); return -1; } printf("Successful "); wclosedir(dirName); return 0; }Output
Successful
See also: wreaddir() wtelldir() wseekdir() readdir() rewinddir() telldir() seekdir()
IMPORT_C int | waccess | ( | const wchar_t * | fn, |
int | flags | |||
) |
The waccess system call checks the accessibility of the file named by the fn argument for the access permissions indicated by the flags argument. The value of flags is either the bitwise-inclusive OR of the access permissions to be checked (R_OK for read permission, W_OK for write permission, and X_OK for execute/search permission), or the existence test ( F_OK. )
For additional information, see the File access Permission section of intro . X_OK, the file may not actually have execute permission bits set. Likewise for R_OK and W_OK.
Note that as execute-bit for files is not supported waccess system call for X_OK is undefined.
/* Detailed description: This sample code checks read-ok accessibility of file Example.c Precondtions: Example.txt file should be present in working directory. */ #include <unistd.h> #include <wchar.h> int main() { if(waccess("Example.c" ,R_OK) < 0) { printf("Read operation on the file is not permitted ") ; return -1 ; } printf("Read operation permitted on Example.c file ") ; return 0 ; }Output
Read operation permitted on Example.c file
Capability | |
---|---|
Deferred | RFs::Entry(const TDesC16&, TEntry&) |
IMPORT_C int | wcreat | ( | const wchar_t * | file, |
mode_t | mode | |||
) |
This interface is made obsolete by: wopen
The wcreat function is the same as: wopen(path, O_CREAT | O_TRUNC | O_WRONLY, mode);
Notes:
Creating a new file doesn't alter the time stamp of the parent directory.
The newly created entry has two time stamps: access and modification. Both are initially the same.
Symbian OS does not support a creation time stamp.
/* Detailed description : This test code demonstrates wcreat api usage, it creates a in current working directory(if file exists then it is truncated. *Preconditions : None */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <wchar.h> int main() { int fd = 0; fd = wcreat(L"Example.txt" , 0666); if(fd < 0 ) { printf("File creation failed "); return -1; } printf("Example.txt file created "); return 0; }Output
Example.txt file created
See also: wopen()
See also: wclosedir() wopendir() wreaddir()
Parameters | |
---|---|
index | Refer to wtelldir() for the documentation |
The wtelldir function returns the current location associated with the named directory stream . Values returned by wtelldir are good only for the lifetime of the DIR pointer, dirp , from which they are derived. If the directory is closed and then reopened, prior values returned by wtelldir will no longer be valid.
The wseekdir function sets the position of the next wreaddir operation on the directory stream . The new position reverts to the one associated with the directory stream when the wtelldir operation was performed.
#include <dirent.h> #include<wchar.h> #include<stdio.h> #include<unistd.h> #include<sys/types.h> void wTest() { //declare the variables for dir pointer and dir position. wchar_t *dirName =L"c: est_wseekdir"; off_t dirpos; // Open the directory WDIR *dirp = wopendir (dirName); if(dirp != NULL) { //read the directory. wreaddir(dirp); wreaddir(dirp); //get the dirp pointer position by calling telldir API. dirpos = wtelldir(dirp); //print the position of the dirp pointer. printf("dirp is pointing at position %ld.",dirpos); wseekdir(dirp , 0) ; wreaddir(dirp); dirpos = wtelldir(dirp); //print the position of the dirp pointer. printf("dirp is pointing at position %ld.",dirpos); } }
Output
Dirp is pointing at position 2. Dirp is pointing at position 1.
See also: wclosedir() wopendir() wreaddir()
Parameters | |
---|---|
dp | Note: This description also covers the following functions - wseekdir() |
IMPORT_C WDIR * | wopendir | ( | const wchar_t * | _path | ) |
The wopendir function opens the directory given by the wide-character name _wpath, associates a directory stream with it and positions it at the first entry and returns a pointer to be used to identify the directory stream in subsequent operations. The pointer NULL is returned if _wpath cannot be accessed, or if it cannot malloc enough memory to hold the whole thing.
/* Detailed description: This test code demonstrates usage of wopendir system call, open directory wide name "test". Preconditions: Expects "test" directory to be present in the current working directory. */ #include <wchar.h> #include <stdio.h> #include <dirent.h> int main() { WDIR *WDirHandle; if(!(WDirHandle = wopendir(L"test") ) ) { printf("Failed to open directory test "); return -1; } printf("Directory test opened "); wclosedir(WDirHandle); return 0; }Output
Directory test opened
IMPORT_C wchar_t * | wcsupr | ( | wchar_t * | wcs | ) |
#include <wchar.h> /* Illustrates how to use wcsupr API */ int example_wcsupr(void) { /* input string which needs to be converted to upper-case */ wchar_t src[9]=L"testcase"; /* expected result string */ wchar_t *exp=L"TESTCASE"; wchar_t *res = NULL; /* function call to convert the src to upper-case */ res = wcsupr(src); /* compare res string with exp string, if they are equal then return 0, else return -1 */ if( res != NULL && !wcscmp(res,exp)) return 0; else return -1; }
See also: strupr() towupper() towlower() wcslwr()
Parameters | |
---|---|
wcs | The wcsupr() function converts each letter from the wide-character string wcs to upper-case. The conversion is determined by the LC_CTYPE category setting of the locale. Other characters are not affected. It returns a pointer to the altered string. Because the modification is done in place, the pointer returned is the same as the pointer passed as the input argument. |
IMPORT_C wchar_t * | wcslwr | ( | wchar_t * | wcs | ) |
#include <wchar.h> /* Illustrates how to use wcslwr API */ int example_wcslwr(void) { /* input string which needs to be converted to lower-case */ wchar_t src[9]=L"TESTCASE"; /* expected result string */ wchar_t *exp=L"testcase"; wchar_t *res = NULL; /* function call to convert the src to lower-case */ res = wcslwr(src); /* compare res string with exp string, if they are equal then return 0, else return -1 */ if( res != NULL && !wcscmp(res,exp)) return 0; else return -1; }
See also: strupr() strlwr() towupper() towlower() wcsupr()
Parameters | |
---|---|
wcs | The wcslwr() function converts each letter from the wide-character string wcs to lower-case. The conversion is determined by the LC_CTYPE category setting of the locale. Other characters are not affected. It returns a pointer to the altered string. Because the modification is done in place, the pointer returned is the same as the pointer passed as the input argument. |
IMPORT_C wchar_t * | wcsset | ( | wchar_t * | wcs, |
wchar_t | wc | |||
) |
#include <wchar.h> /* Illustrates how to use wcsset API */ int example_wcsset(void) { /* input string which needs to be set to the character specified by wc. */ wchar_t wcs[9]=L"kohinoor"; wcgar_t wc = L'K'; /* expected result string */ wchar_t *exp=L"KKKKKKKK"; wchar_t *res = NULL; /* function call to set all the chars in wcs to wc*/ res = wcsset(wcs, wc); /* compare res string with exp string, if they are equal then return 0, else return -1 */ if( res != NULL && !wcscmp(res,exp)) return 0; else return -1; }
See also: strset() wcsnset()
Parameters | |
---|---|
wc | The wcsset() function sets all the characters of the wide-character string wcs to the wide-character specified by wc, except the terminating null character. It returns a pointer to the altered string, which is same as the source string passed to wcsset as it is modified in place. |
IMPORT_C wchar_t * | wcsnset | ( | wchar_t * | wcs, |
wchar_t | wc, | |||
size_t | maxSize | |||
) |
#include <wchar.h> /* Illustrates how to use wcsnset API */ int example_wcsnset(void) { /* input string which needs to be set to the character specified by wc. */ wchar_t wcs[9]=L"kohinoor"; wcgar_t wc = L'K'; int n = 3; /* expected result string */ wchar_t *exp=L"KKKinoor"; wchar_t *res = NULL; /* function call to set first n chars in wcs to wc*/ res = wcsnset(wcs, wc, n); /* compare res string with exp string, if they are equal then return 0, else return -1 */ if( res != NULL && !wcscmp(res,exp)) return 0; else return -1; }
See also: strset() wcsset()
Parameters | |
---|---|
maxSize | The wcsnset() function sets first maxSize characters of the wide-character string wcs to the wide-character specified by wc. If maxSize is greater than the length of wcs, then length of wcs is used instead of maxSize. It returns a pointer to the altered string, which is same as the source string passed to wcsnset as it is modified in place. |
IMPORT_C wchar_t * | wcsrev | ( | wchar_t * | wcs | ) |
#include <wchar.h> /* Illustrates how to use wcsrev API */ int example_wcsrev(void) { /* input string which needs to be reverted. */ wchar_t src[9]=L"kohinoor"; /* expected result string */ wchar_t *exp=L"roonihok"; wchar_t *res = NULL; /* function call to revert the src */ res = wcsrev(src); /* compare res string with exp string, if they are equal then return 0, else return -1 */ if( res != NULL && !wcscmp(res,exp)) return 0; else return -1; }
See also: strrev()
Parameters | |
---|---|
wcs | The wcsrev() function reverses the order of the characters in the wide-character string wcs. The terminating null character remains in place.The arguments and return value of wcsrev are wide-character strings; It returns a pointer to the altered string. the pointer returned is the same as the pointer passed as the input argument. |
IMPORT_C int | wcsicmp | ( | const wchar_t * | wcs1, |
const wchar_t * | wcs2 | |||
) |
#include <wchar.h> /* Illustrates how to use wcsicmp API */ int example_wcsicmp(void) { /* input strings which needs to be compared */ wchar_t *ws1=L"testcasecmp"; wchar_t *ws2=L"TESTCASECMP"; /* function call to compare the two strings which */ /* differ in case */ int retval = wcsicmp(ws1,ws2); /* returns 0 if they are equal or > 1 */ /* if first string is greater than second string else -1 */ return retval; }
See also: strcasecmp() wcscasecmp()
Parameters | |
---|---|
wcs2 | The wcsicmp() function compares the two null-terminated wide-character strings wcs1 and wcs2, by converting them into lower-case. It returns an integer value indicating the status of the comparision. |
IMPORT_C wchar_t * | wstrdate | ( | const wchar_t * | dateStr | ) |
#include <wchar.h> /* Illustrates how to use wstrdate API */ int example_wstrdate(void) { /* input string which is updated with the system date. */ wchar_t datestr[20]; wchar_t *res = NULL; /* function call to get the system date in wide-char format */ res = wstrdate(datestr); if( !res && !datestr) { printf(" res - %s and datestr - %s",res, datestr); return 0; } else return -1; }
Output
res - 21/11/2006 and datestr - 21/11/2006
Errors:
The wstrdate function will fail if: [EFAULT] The supplied argument datestr is invalid.
See also: strdate() strftime() strptime() wstrtime()
Parameters | |
---|---|
dateStr | The wstrdate() function gets the system date and converts it into wide-character string. This copies the current system date into the string pointed by datestr in the format dd/mm/yyyy, the datestr buffer must be atleast 11 bytes long. It returns a pointer to the datestr string. Because the modification is done in place, the pointer returned is the same as the pointer passed as the input argument. |
IMPORT_C wchar_t * | wstrtime | ( | const wchar_t * | timeStr | ) |
#include <wchar.h> /* Illustrates how to use wstrtime API */ int example_wstrtime(void) { /* input string which is updated with the system time. */ wchar_t timestr[20]; wchar_t *res = NULL; /* function call to get the system time in wide-char format */ res = wstrtime(timestr); if( !res && !timestr) { printf(" res - %s and timestr - %s",res, timestr); return 0; } else return -1; }
Output
res - 15:46:36 and timestr - 15:46:36
See also: strtime() strftime() strptime()
Parameters | |
---|---|
timeStr | The wstrtime() function gets the system time and converts it into wide-character string. This copies the current system time into the string pointed by timestr in the format hh:mm:ss. The timestr buffer must be at least 9 bytes long. The function returns a pointer to the timestr string. Because the modification is done in place, the pointer returned is the same as the pointer passed as the input argument. |
IMPORT_C FILE * | wfdopen | ( | int | fd, |
const wchar_t * | mode | |||
) |
The wfdopen function associates a stream with the existing file descriptor, fd. The mode argument is used just as in the function wfopen .
The mode of the stream must be compatible with the mode of the file descriptor. In other words the type specified by the stream must be allowed by the file access mode of the open file. When the stream is closed via fclose, fd is also closed.
Errors:
[EINVAL] The mode argument to wfdopen, was invalid. The function wfdopen may also fail and set errno for any of the errors specified for the routine wopen.
Limitations:
All the limitations that apply to wfopen apply to wfdopen also.
The wfreopen function opens the file whose name is the string pointed to by file and associates the stream pointed to by fp with it. The original stream (if it exists) is closed. The mode argument is used just as in the wfopen function.
If the file argument is NULL, wfreopen attempts to re-open the file associated with fp with a new mode. The new mode must be compatible with the mode that the stream was originally opened with: Streams originally opened with mode "r" can only be reopened with that same mode. Streams originally opened with mode "a" can be reopened with the same mode, or mode "w." Streams originally opened with mode "w" can be reopened with the same mode, or mode "a." Streams originally opened with mode "r+," "w+," or "a+" can be reopened with any mode.
The primary use of the wfreopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout).
/* wfreopen example: redirecting stdout */ #include <stdio.h> #include <wchar.h> int main () { wfreopen (L"c:\myfile.txt",L"w",stdout); printf ("This sentence is redirected to a file."); fclose (stdout); return 0; }Output:
Contents of myfile.txt:This sentence is redirected to a file. The output here is redirected from stdout to file myfile.txt
Limitations:
All the limitations that apply to wfopen apply to wfreopen also.
IMPORT_C wchar_t * | getws | ( | wchar_t * | str | ) |
The getws function is equivalent to fgetws with an infinite size and a stream of stdin, except that the wide character newline(if any) is not stored in the string. It is the caller's responsibility to ensure that the input line, if any, is sufficiently short to fit in the string.
#include <stdio.h> #include <wchar.h> /* Illustrates how to use getws API */ int example_getws() { FILE* stdop = freopen("c:\stdop","w+",stdout); FILE* stdip = freopen("c:\stdip","w+",stdin); FILE* stder = freopen("c:\stder","w+",stderr); wchar_t* buf = (wchar_t*)malloc(sizeof(wchar_t)*50); wchar_t s[100]; int ret = 0; size_t size = fwrite("abdcef ", 1, 6, stdip); //write to stdin fclose(stdip); stdip = freopen("c:\stdip","r", stdin); getws(s); // read a line (from stdin) putws(s); //write to stdout fclose(stdop); stdop = freopen("c:\stdop","r", stdout); fgetws(buf,7,stdop); //read from stdout if(wcsncmp(s, buf,6)) { ret = -1; } fclose(stdin); fclose(stder); fclose(stdop); remove("c:\stdop"); remove("c:\stdip"); remove("c:\stder"); return ret; }Output
abcdef abcdef
Security considerations:
The getws function cannot be used securely. Because of its lack of bounds checking, and the inability for the calling program to determine reliably the length of the next incoming line, the use of this function enables malicious users to arbitrarily change a running program's functionality through a buffer overflow attack. It is strongly suggested that the fgetws function be used in all cases.
IMPORT_C int | wremove | ( | const wchar_t * | file | ) |
The wremove function removes the file or directory specified by the wide character name referred by file.
If file specifies a directory, wremove (file); is the equivalent of wrmdir (file); Otherwise, it is the equivalent of wunlink (file);
/* this program shows deleting a file using wremove */ #include <stdio.h> int main() { wchar_t* name = L"C:\input.txt"; FILE *fp = wfopen(name, L"w+"); if (fp == NULL) { printf ("fopen failed "); return -1; } fprintf(fp,"hello world"); fclose(fp); wremove(name); fp=wfopen(name,L"r"); if (fp == NULL) { printf ("file has been deleted already "); } else { printf("wremove failed "); return -1; } return 0; }Output
file has been deleted already
IMPORT_C int | putws | ( | wchar_t * | str | ) |
The putws function writes the wide character string pointed to by str to the stdout stream.
#include <stdio.h> #include <wchar.h> /* Illustrates how to use putws API */ int example_putws() { wchar_t buf[12]; FILE* op; FILE* stdop = freopen("c:\stdop","w+",stdout); putws(L"Hello World"); //write to stdout fclose(stdop); op = freopen("c:\stdop","r",stdout); fgetws(buf, 12, op); //read from stdout fclose(stdop); remove("c:\stdop"); if(!(wcsncmp(L"Hello World", buf, 11))) { return 0; } return -1; } }
IMPORT_C wchar_t * | wctime | ( | const time_t * | ) |
The function wctime takes a time value representing the time in seconds since the Epoch (00:00:00 UTC, January 1, 1970) as an argument. See time
The function wctime adjusts the time value for the current time zone in the same manner as localtime and returns a pointer to a 26-wide character string of the form: Thu Nov 24 18:22:48 1986 \0 All the fields have constant width.
The function wasctime converts the broken down time in the structure tm , pointed at by *tm , to the form shown in the example above.
External declarations as well as the tm structure definition are in the #include <time.h> include file. The tm structure includes at least the following fields: int tm_sec;/* seconds (0 - 60) */ int tm_min;/* minutes (0 - 59) */ int tm_hour;/* hours (0 - 23) */ int tm_mday;/* day of month (1 - 31) */ int tm_mon;/* month of year (0 - 11) */ int tm_year;/* year - 1900 */ int tm_wday;/* day of week (Sunday = 0) */ int tm_yday;/* day of year (0 - 365) */ int tm_isdst;/* is summer time in effect? */ char *tm_zone;/* abbreviation of timezone name */ long tm_gmtoff;/* offset from UTC in seconds */
The field tm_isdst is non-zero if summer time is in effect.
The field tm_gmtoff is the offset (in seconds) of the time represented from UTC, with positive values indicating east of the Prime Meridian.
//Example usage of wasctime and wctime: #include <time.h> #include <stdio.h> int main(){ time_t t; struct tm *timeptr; wchar_t* wasc_time; t = time (NULL); //Get current time in seconds from Epoc //Fill tm struct w.r.t localtime using localtime timeptr = localtime (&t;); //Use this to convert it to a string indicating time w.r.t localtime wasc_time = wasctime (timeptr); wprintf (L"Time from wasctime w.r.t localtime : %s", wasc_time); wprintf(L"Time from wctime w.r.t localtime : %s0, wctime(&t;) ); return 0; }Output
Time from wasctime w.r.t localtime : Thu Jun 22 10:42:27 2006 Time from wctime w.r.t localtime : Thu Jun 22 10:42:27 2006
Bugs:
These functions leave their result in an internal static object and return a pointer to that object. Subsequent calls to these functions will modify the same object. The C Standard provides no mechanism for a program to modify its current local timezone setting and the POSIX -standard method is not reentrant. (However, thread-safe implementations are provided in the POSIX threaded environment.) The tm_zone field of a returned tm structure points to a static array of characters which will also be overwritten by any subsequent calls (as well as by subsequent call to tzset )
Parameters | |
---|---|
Note: This description also covers the following functions - wasctime() |
IMPORT_C wchar_t * | wsetlocale | ( | int | , |
const wchar_t * | ||||
) |
The wsetlocale function sets the C library's notion of natural language formatting style for particular sets of routines. Each such style is called a 'locale' and is invoked using an appropriate name passed as a wide char string.
The wsetlocale function can recognise several categories of routines. The categories and the sets of routines recognised by wsetlocale are listed below:
LC_ALL Set the entire locale generically. LC_COLLATE Set a locale for string collation routines. Currently locale setting does not have effect on this category. LC_CTYPE This controls recognition of upper and lower case, alphabetic or non-alphabetic characters, and so on. Currently locale setting does not have effect on this category. LC_MESSAGES Set a locale for message catalogs. Currently this category is not supported. LC_MONETARY Set a locale for formatting monetary values; this affects the localeconv function. LC_NUMERIC Set a locale for formatting numbers. This controls the formatting of decimal points in input and output of floating point numbers in functions such as printf and scanf, as well as values returned by localeconv. LC_TIME Set a locale for formatting dates and times using the strftime function.
Only three locales are defined by default, the empty string which denotes the native environment, the C and the POSIX locales, which denote the C language environment. A locale argument of NULL causes wsetlocale to return the current locale. By default, C programs start in the C locale. The only functions in the library that set the locale are wsetlocale and setlocale; the locale is never changed as a side effect of some other routine.
#include<stdio.h> #include<wchar.h> int main() { //Set the locale to UK English wchar_t* locale = wsetlocale(LC_ALL,L"en_GB.ISO-8859-1"); //Check whether locale setting is succesful or not if(NULL != locale) { wprintf(L"Locale setting is successful "); wprintf(L"Locale is set to %s ", locale); } else { wprintf(L"Locale setting failed "); } return 0; }Output
Locale setting is successful Locale is set to en_GB.ISO-8859-1
See also: setlocale() localeconv() nl_langinfo()
IMPORT_C void | wperror | ( | const wchar_t * | ) |
The functions wcserror and wperror look up the error message string corresponding to an error number.
The function wcserror function accepts an error number argument errnum (error number) and returns a pointer to the corresponding message string.
The function wperror finds the error message corresponding to the current value of the global variable errno and writes it, followed by a newline, to the standard error file descriptor. If the argument s is non- NULL and does not point to the null character, this wide char string is prepended to the message string and separated from it by a colon and space (": "); otherwise, only the error message string is printed.
If the error number is not recognized, these functions return an error message string containing "Unknown error: " followed by the error number in decimal. The wcserror function returns EINVAL as a warning. Error numbers recognized by this implementation fall in the range 0 <errnum <sys_nerr .
If insufficient storage is provided in strerrbuf (as specified in buflen ) to contain the error string, wcserror returns ERANGE and strerrbuf will contain an error message that has been truncated and NUL terminated to fit the length specified by buflen .
#include <string.h> #include <stdio.h> #include <errno.h> #include <wchar.h> int main() { wchar_t *ptr = wcserror(ERANGE); wprintf(L"wcserror(ERANGE) = %s ",ptr); return 0; }Output
wcserror(ERANGE) = Numerical result out of range
See also: perror() strerror()
Bugs:
For unknown error numbers, the function wcserror will return its result in a static buffer which may be overwritten by subsequent calls. The return type for wcserror is missing a type-qualifier. The type-qualifier must be const wchar_t * .
Parameters | |
---|---|
Note: This description also covers the following functions - wcserror() |
IMPORT_C wchar_t * | wcserror | ( | int | num | ) |
See also: perror() strerror()
Parameters | |
---|---|
num | Refer to wperror() for the documentation |
IMPORT_C int | wfindnext | ( | intptr_t | , |
struct _wfinddata_t * | ||||
) |
Parameters | |
---|---|
Refer to wfindfirst() for the documentation |
IMPORT_C intptr_t | wfindfirst | ( | const wchar_t * | , |
struct _wfinddata_t * | ||||
) |
Errors:
[EINVAL] Invalid parameter: filespec or fileinfo was NULL. Or, the operating system returned an unexpected error. [ENOENT] File specification that could not be matched.
The wfindfirst function provides information about the first instance of a file name that matches the file specified in the filespec argument. Any wildcard combination supported by the host operating system can be used in filespec. File information is returned in a _wfinddata_t structure, defined in wchar.h. The _wfinddata_t structure includes the following elements.
unsigned attrib File attribute
time_t time_create Time of file creation (-1L for Symbian OS)
time_t time_access Time of the last file access (-1L for Symbian OS)
time_t time_write Time of the last write to file
size_t size Length of the file in bytes
wchar_t name[260] Null-terminated name of matched file/directory, without the path
This attribute is returned in the attrib field of the _wfinddata_t structure and can have the following values (defined in wchar.h).
_A_ARCH Archive. _A_HIDDEN Hidden file. _A_NORMAL Normal. _A_RDONLY Read-only. _A_SYSTEM System file.
The wfindnext finds the next name, if any, that matches the filespec argument in a previous call to wfindfirst and then alters the fileinfo structure contents accordingly.
The findclose closes the specified search handle and releases the associated resources.
#include <wchar.h> int main( void ) { struct _wfinddata_t c_file; intptr_t hFile; // Find .txt file in current directory if( (hFile = wfindfirst( L"*.txt", &c;_file )) == -1L ) printf( "No *.txt files in current directory" ); else { int i = 1; do { wprintf( L" File %d = %s ",i,c_file.name); i++; } while( wfindnext( hFile, &c;_file ) == 0 ); findclose( hFile ); } return 0 ; }Output
File 1 = test1.txt File 2 = test2.txt
Bugs:
Does not support any attribute to find out the sub-directories (i.e., attribute _A_SUBDIR not supported).
Parameters | |
---|---|
Note: This description also covers the following functions - wfindnext() findclose() |
IMPORT_C int | wcsnicmp | ( | const wchar_t * | wcs1, |
const wchar_t * | wcs2, | |||
size_t | n | |||
) |
#include <wchar.h> /* Illustrates how to use wcsnicmp API */ int example_wcsnicmp(void) { /* input strings which needs to be compared */ wchar_t *ws1=L"testcasecmp"; wchar_t *ws2=L"TESTCASECMP"; /* function call to compare the two strings which */ /* differ in case */ int retval = wcsnicmp(ws1,ws2,12); /* returns 0 if they are equal or > 1 */ /* if first string is greater than second string else -1 */ return retval; }
See also: strcasecmp() wcscasecmp() wcsncasecmp()
Parameters | |
---|---|
n | The wcsnicmp() function compares the first n characters from two null-terminated wide-character strings wcs1 and wcs2, by converting them into lower-case. It returns an integer value indicating the status of the comparision. |
IMPORT_C int | wcsicoll | ( | const wchar_t * | wcs1, |
const wchar_t * | wcs2 | |||
) |
The wcsicoll function compares the null-terminated strings wcs1 and wcs2 according to the current locale collation order, by ignoring the case. In the "C" locale, wcsicoll is equivalent to wcsicmp .
#include <wchar.h> /* Illustrates how to use wcsicoll API */ int example_wcsicoll (void) { /* compares the two strings */ if( wcsicoll(L"abcdef",L"abcdeg") != L'f'-L'g') return -1; return 0; }
Limitations:
The current implementation of wcsicoll is not affected by the LC_CTYPE category of the current locale. It is equivalent to wcsicmp in this implementation.
See also: setlocale() strcoll() wcsicmp() wcsxfrm()
Bugs:
The current implementation of wcsicoll only works in single-byte LC_CTYPE locales, and falls back to using wcsicmp in locales with extended character sets.
IMPORT_C int | wcsncoll | ( | const wchar_t * | wcs1, |
const wchar_t * | wcs2, | |||
size_t | n | |||
) |
The wcsncoll function compares the first n chars from the two null-terminated strings wcs1 and wcs2 according to the current locale collation order. In the "C" locale, wcsncoll is equivalent to wcscmp .
#include <wchar.h> /* Illustrates how to use wcsncoll API */ int example_wcsncoll (void) { /* compares the two strings */ if( wcsncoll(L"abcdef",L"abcdeg",7) != L'f'-L'g') return -1; return 0; }
Limitations:
The current implementation of wcsncoll is not affected by the LC_CTYPE category of the current locale. It is equivalent to wcscoll in this implementation.
See also: setlocale() strcoll() wcscoll() wcsncmp() wcsxfrm()
Bugs:
The current implementation of wcsncoll only works in single-byte LC_CTYPE locales, and falls back to using wcscoll in locales with extended character sets.
IMPORT_C int | wcsnicoll | ( | const wchar_t * | wcs1, |
const wchar_t * | wcs2, | |||
size_t | n | |||
) |
The wcsnicoll function compares the null-terminated strings wcs1 and wcs2 according to the current locale collation order. In the "C" locale, wcsnicoll is equivalent to wcsncasecmp .
#include <wchar.h> /* Illustrates how to use wcsnicoll API */ int example_wcsnicoll (void) { /* compares the two strings */ if( wcsnicoll(L"abcdef",L"abcdeg",7) != L'f'-L'g') return -1; return 0; }
Limitations:
The current implementation of wcsnicoll is not affected by the LC_CTYPE category of the current locale. It is equivalent to wcsncasecmp in this implementation.
See also: setlocale() strcoll() wcscmp() wcsxfrm() wcsncasecmp()
Bugs:
The current implementation of wcsnicoll only works in single-byte LC_CTYPE locales, and falls back to using wcsncoll in locales with extended character sets.
IMPORT_C wchar_t * | wtmpnam | ( | wchar_t * | s | ) |
The wtmpnam function returns a pointer to a file name in the P_tmpdir directory which did not reference an existing file at some indeterminate point in the past. P_tmpdir is defined in the include file stdio.h . If the argument s is non-NULL, the file name is copied to the buffer it references. Otherwise, the file name is copied to a static buffer. In either case, wtmpnam returns a pointer to the file name.
The buffer referenced by s is expected to be at least L_tmpnam bytes in length. L_tmpnam is defined in the include file stdio.h .
The environment variable TMPDIR (if set), the argument tmpdir (if non-NULL), the directory P_tmpdir , and the directory /tmp are tried in that order as directories in which to store the temporary file.
#include<stdio.h> //wtmpnam #include<sys/stat.h> //S_IWUSR #include<errno.h> //errno int main() { //create a directory c:\system emp wmkdir(L"c:\system\temp", S_IWUSR); wchar_t wbuf[L_tmpnam]; wchar_t rbuf[10]; //call wtmpnam() to create a file wchar_t *rval = wtmpnam(wbuf); errno = 0; //open the file with the name returned by wtmpnam() FILE *fp = wfopen(buf, L"w"); if (fp == NULL) { printf("fopen of file returned by wtmpnam() failed - errno %d ", errno); return -1; } if(fp) { fwprintf(fp, L"%ls", L"check"); fclose(fp); } fp = wfopen(buf, L"r"); if(fp) { fwscanf(fp, L"%ls", rbuf); fclose(fp); } printf("read from file: %ls ", rbuf); printf("argument buf: %ls ", buf); printf("return value: %ls ", rval); return 0; }Output
read from file: check argument buf: /System/temp/tmp.0.U9UPTx return value: /System/temp/tmp.0.U9UPTx
See also: mktemp() tmpnam()