Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

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

Common functions

1. Wide-character input/output

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

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

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

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