C++ Standard Library | Tutorial for Beginners\n\n
C++ Standard Library |
\n \n
\n`` is C++ A header file in the standard library that provides functions for handling wide characters (`wchar_t`๏ผand wide string functions. Most of these functions come from C standard library's ``๏ผUsed for processing wide character input/output, memory operations, string operations and other wide character related functions.\n\n## Syntax\n\n`cwchar` Functions defined in header file usually have names similar to standard character processing functions, but starting with `w` beginning, for example `wprintf`ใ`wscanf` etc. The parameters and return types of these functions also differ from corresponding standard character functions, they use wide character type `wchar_t`ใ\n\n### Basic Types\n\n* `wchar_t`๏ผWide character type, used to store wide characters.\n* `wint_t`๏ผUsed to store return value of wide character functions.\n\n## Common functions\n\n### 1. **Wide character input/output**\n\n* `fgetwc`๏ผRead wide characters from file stream.\n* `fputwc`๏ผWrite wide character to file stream.\n* `fgetws`๏ผRead wide string from file stream.\n* `fputws`๏ผWrite wide string to file stream.\n\n## Example\n\n```cpp\n#include \n#include \n\nint main(){\n // Use fputws and fgetws for wide string input/output\n const wchar_t* filename = L"example.txt";\n FILE* file = std::fopen("example.txt", "w");\n if(file){\n std::fputws(L"Hello, World!n", file);\n std::fclose(file);\n }\n file = std::fopen("example.txt", "r");\n if(file){\n wchar_t buffer;\n if(std::fgetws(buffer, 256, file)){\n std::wcout << L"Read from file: " << buffer;\n }\n std::fclose(file);\n }\n return 0;\n}\n\nThe output result is:\n\nRead from file: Hello, World!\n\n### 2. **Wide character and wide string operations**\n\n* `wcscpy`๏ผCopy wide string.\n* `wcslen`๏ผGet Wide string lengthใ\n* `wcscmp`๏ผCompare wide strings.\n* `wcsncpy`๏ผCopy wide string of specified length.\n\n## Example\n\n```cpp\n#include \n#include \n\nint main(){\n wchar_t str1 = L"Hello";\n wchar_t str2 = L"World";\n // Wide string copy\n std::wcscpy(str1, L"Hello, World!");\n std::wcout << L"Copied string: " << str1 << std::endl;\n // Wide string length\n size_t len = std::wcslen(str1);\n std::wcout << L"Length of string: " << len << std::endl;\n // Wide string comparison\n int result = std::wcscmp(str1, str2);\n std::wcout << L"Comparison result: " << result << std::endl;\n // Partial copy of wide string\n std::wcsncpy(str2, str1, 5);\n str2 = L''; // Ensure null-terminated\n std::wcout << L"Partially copied string: " << str2 << std::endl;\n return 0;\n}\n\nThe output result is:\n\nCopied string: Hello,\n\n### 3. **Wide character classification and conversion**\n\n* `iswalpha`๏ผCheck if wide character is a letter.\n* `iswdigit`๏ผCheck if wide character is a digitใ\n* `towlower`๏ผConvert wide character to lowercase.\n* `towupper`๏ผConvert wide character to uppercase.\n\n## Example\n\n```cpp\n#include \n#include \n\nint main(){\n wchar_t ch = L'A';\n // Check if wide character is a letter\n if(std::iswalpha(ch)){\n std::wcout << ch << L" is an alphabetic character." << std::endl;\n }\n // Check if wide character is a digit\n ch = L'9';\n if(std::iswdigit(ch)){\n std::wcout << ch << L" is a digit." << std::endl;\n }\n // Convert wide character to lowercase\n ch = L'G';\n wchar_t lower_ch = std::towlower(ch);\n std::wcout << L"Lowercase of " << ch << L" is " << lower_ch << std::endl;\n // Convert wide character to uppercase\n ch = L'a';\n wchar_t upper_ch = std::towupper(ch);\n std::wcout << L"Uppercase of " << ch << L" is " << upper_ch << std::endl;\n return 0;\n}\n\nThe output result is:\n\nA is an alphabetic character.9 is a digit.Lowercase of G is g Uppercase of a is A\n\n### 4. **Input/output of wide characters and wide strings**\n\n* `wprintf`๏ผFormatted output of wide characters.\n* `wscanf`๏ผFormatted input of wide characters.\n* `swprintf`๏ผWrite formatted wide characters to wide stringใ\n* `swscanf`๏ผRead formatted wide characters from wide stringใ\n\n## Example\n\n```cpp\n#include \n#include \n\nint main(){\n wchar_t buffer;\n // Formatted output of wide characters\n std::wprintf(L"Formatted output: %d %sn", 42, L"Hello");\n // Formatted input of wide characters\n std::wprintf(L"Enter a number and a string: ");\n std::wscanf(L"%d %ls", &buffer);\n std::wprintf(L"You entered: %lsn", buffer);\n // Write formatted wide characters to wide string\n std::swprintf(buffer, 100, L"Formatted %d %s", 42, L"output");\n std::wcout << L"Buffer: " << buffer << std::endl;\n // Read formatted wide characters from wide string\n int number;\n wchar_t word;\n std::swscanf(buffer, L"Formatted %d %s", &number, word);\n std::wcout << L"Parsed number: " << number << L", word: " << word << std::endl;\n return 0;\n}\n\nThe output result is:\n\nFormatted output: 42 H Enter a number and a string: You entered: Buffer: Formatted 42 o Parsed number: 42, word: o\n\n