📅  最后修改于: 2023-12-03 15:13:54.228000             🧑  作者: Mango
The fputwc()
function is a C++ library function that writes a wide character (wchar_t data type) to a stream.
The syntax for the fputwc()
function is:
wint_t fputwc (wchar_t character, FILE* stream);
Where:
character
is the wide character to be written to the stream.stream
is the pointer to a FILE object that specifies the output stream.The function returns the wide character written to the stream on success, and WEOF
on failure.
Here is an example program that uses fputwc()
:
#include <cstdio>
#include <cwchar>
int main() {
FILE* fp;
const wchar_t text[] = L"Hello World!";
fp = std::fopen("output.txt", "w");
if (fp != nullptr) {
for (const auto & c : text) {
std::fputwc(c, fp);
}
std::fclose(fp);
}
return 0;
}
In this example program, we first define a wide character array text
, which contains the string "Hello World!" in wide character format. We then use std::fopen()
to open the file "output.txt" for writing, and check if the file was successfully opened. If yes, we use a for
loop to iterate over each character in text
, and for each character, we use std::fputwc()
to write it to the output stream fp
. Finally, we use std::fclose()
to close the output stream.
Unlike the fputc()
function, which writes only one byte at a time, the fputwc()
function writes one or more bytes for each wide character, depending on the size of the wide character representation on the system. Therefore, the amount of data written to the output stream for each call to fputwc()
may be larger than one byte.