📜  C++ fputws()(1)

📅  最后修改于: 2023-12-03 14:39:49.704000             🧑  作者: Mango

C++ fputws()

Introduction

fputws() is a function in C++ programming language that is used to write a wide string to a file. It appends the string to the file specified by the file pointer.

The fputws() function is usually used to write wide strings to a file in a wide character format.

Syntax

The syntax for the fputws() function is as follows:

int fputws(const wchar_t* str, FILE* stream);
  • str: The wide string to be written to the file.
  • stream: The file pointer to the output file.
Return Value

The fputws() function returns the non-negative value if successful, otherwise it returns EOF if an error occurs.

Example

Here is an example that demonstrates the usage of fputws() function:

#include <iostream>
#include <stdio.h>

int main() {
    FILE* file = fopen("output.txt", "w");
    if (file != NULL) {
        const wchar_t* str = L"Hello, World!"; // Wide string
        if (fputws(str, file) != EOF) { // Write wide string to file
            std::cout << "Wide string successfully written to file." << std::endl;
        } else {
            std::cout << "Error writing wide string to file." << std::endl;
        }
        fclose(file);
    } else {
        std::cout << "Error opening file." << std::endl;
    }
    return 0;
}

In this example, we first open a file called "output.txt" in write mode using fopen() function. Then, we declare a wide string str with the value "Hello, World!". We pass this wide string and the file pointer to the fputws() function to write the wide string to the file. Finally, we close the file using fclose() function.

If the file is successfully opened and the wide string is written to the file, it will display the message "Wide string successfully written to file." Otherwise, it will display the message "Error writing wide string to file." If there is an error opening the file, it will display the message "Error opening file."

Conclusion

The fputws() function in C++ is a useful function for writing wide strings to a file. It allows you to write wide characters to a file. By using this function, you can easily store wide strings in a file for further processing or retrieval.