The C++ standard library
is a header file in the C++ standard library that provides functions for handling wide characters (wchar_t) and wide strings. Most of these functions come from the C standard library's , and are used for wide-character input/output, memory operations, string operations, and other wide-character-related functionality.
Syntax
cwchar Functions defined in the header usually have names similar to standard character handling functions, but start with w , for example wprintf, wscanf , etc. The parameters and return types of these functions also differ from the corresponding standard character functions; they use the wide-character type wchar_t.
Basic types
wchar_t: Wide character type, used to store wide characters.wint_t: Used to store the return value of wide-character functions.
Common functions
1. Wide-character input/output
fgetwc: Reads a wide character from a file stream.fputwc: Writes a wide character to a file stream.fgetws: Reads a wide string from a file stream.fputws: Writes a wide string to a file stream.
Example
#include
#include
int
main
(
)
{
// Use fputws and fgetws for wide-string I/O
const
wchar_t
filename
=
L
"example.txt"
;
FILE
file
=
std
::
fopen
(
"example.txt"
,
"w"
)
;
if
(
file
)
{
std
::
fputws
(
L
"Hello, World!\n"
, file
)
;
std
::
fclose
(
file
)
;
}
file
=
std
::
fopen
(
"example.txt"
,
"r"
)
;
if
(
file
)
{
wchar_t
buffer
[
256
]
;
if
(
std
::
fgetws
(
buffer,
256
, file
)
)
{
std
::
wcout
<<
L
"Read from file: "
<<
buffer
;
}
std
::
fclose
(
file
)
;
}
return
0
;
}
The output is:
Read from file: Hello, 世界!
2. Wide-character and wide-string operations
wcscpy: Copies a wide string.wcslen: Gets the length of a wide string.wcscmp: Compares wide strings.wcsncpy: Copies a wide string of specified length.
Example
#include
#include
int
main
(
)
{
wchar_t
str1
[
100
]
=
L
"Hello"
;
wchar_t
str2
[
100
]
=
L
"World"
;
// Wide string copy
std
::
wcscpy
(
str1,
L
"Hello, World!"
)
;
std
::
wcout
<<
L
"Copied string: "
<<
str1
<<
std
::
endl
;
// Wide string length
size_t
len
=
std
::
wcslen
(
str1
)
;
std
::
wcout
<<
L
"Length of string: "
<<
len
<<
std
::
endl
;
// Wide string compare
int
result
=
std
::
wcscmp
(
str1, str2
)
;
std
::
wcout
<<
L
"Comparison result: "
<<
result
<<
std
::
endl
;
// Wide string partial copy
std
::
wcsncpy
(
str2, str1,
5
)
;
str2
[
5
]
=
L
'\0'
;
// Ensure null-terminated
std
::
wcout
<<
L
"Partially copied string: "
<<
str2
<<
std
::
endl
;
return
0
;
}
The output is:
Copied string: Hello,
3. Wide-character classification and conversion
iswalpha: Checks whether a wide character is a letter.iswdigit: Checks whether a wide character is a digit.towlower: Converts a wide character to lowercase.towupper: Converts a wide character to uppercase.
Example
#include
#include
int
main
(
)
{
wchar_t
ch
=
L
'A'
;
// Check whether a wide character is a letter
if
(
std
::
iswalpha
(
ch
)
)
{
std
::
wcout
<<
ch
<<
L
" is an alphabetic character."
<<
std
::
endl
;
}
// Check whether a wide character is a digit
ch
=
L
'9'
;
if
(
std
::
iswdigit
(
ch
)
)
{
std
::
wcout
<<
ch
<<
L
" is a digit."
<<
std
::
endl
;
}
// Convert wide character to lowercase
ch
=
L
'G'
;
wchar_t
lower_ch
=
std
::
towlower
(
ch
)
;
std
::
wcout
<<
L
"Lowercase of "
<<
ch
<<
L
" is "
<<
lower_ch
<<
std
::
endl
;
// Convert wide character to uppercase
ch
=
L
'a'
;
wchar_t
upper_ch
=
std
::
towupper
(
ch
)
;
std
::
wcout
<<
L
"Uppercase of "
<<
ch
<<
L
" is "
<<
upper_ch
<<
std
::
endl
;
return
0
;
}
The output is:
A is an alphabetic character.
9 is a digit.
Lowercase of G is g
Uppercase of a is A
4. Input/output of wide characters and wide strings
wprintf: Wide-character formatted output.wscanf: Wide-character formatted input.swprintf: Writes formatted wide characters to a wide string.swscanf: Reads formatted wide characters from a wide string.
Example
#include
#include
int
main
(
)
{
wchar_t
buffer
[
100
]
;
// Wide-character formatted output
std
::
wprintf
(
L
"Formatted output: %d %s\n"
,
42
,
L
"Hello"
)
;
// Wide-character formatted input
std
::
wprintf
(
L
"Enter a number and a string: "
)
;
std
::
wscanf
(
L
"%d %ls"
,
&
buffer
)
;
std
::
wprintf
(
L
"You entered: %ls\n"
, buffer
)
;
// Write formatted wide characters to a wide string
std
::
swprintf
(
buffer,
100
,
L
"Formatted %d %s"
,
42
,
L
"output"
)
;
std
::
wcout
<<
L
"Buffer: "
<<
buffer
<<
std
::
endl
;
// Read formatted wide characters from a wide string
int
number
;
wchar_t
word
[
100
]
;
std
::
swscanf
(
buffer,
L
"Formatted %d %s"
,
&
number, word
)
;
std
::
wcout
<<
L
"Parsed number: "
<<
number
<<
L
", word: "
<<
word
<<
std
::
endl
;
return
0
;
}
The output is:
Formatted output: 42 H
Enter a number and a string: runoob
You entered:
Buffer: Formatted 42 o
Parsed number: 42, word: o