What is fwprintf_s in C?
The fwprintf_s fucntion
The function fwprintf_s is defined in the wchar.h standard library and is used to write wide characters to file stream.
The function fwprintf_s is a wide character version of the function fprintf_s. The structure of the fwprintf_s function is as follows:
int fwprintf_s(FILE * stream, const wchar_t * format, ...);
Parameters
streamis a pointer toFILEstructure.formatis a null terminated wide character string....represents optional parameters passed due to theformatstring.
Return value
- A positive integer specifying the number of wide characters successfully written.
- A negative integer if an error occurs.
Example usage of the fwprintf_s function
The following code writes wide characters to a file stream using the fwprintf_s function:
#include<stdio.h>#include<wchar.h>int main() {int i = 10;double j = 5.5;wchar_t n = L'\n';FILE * write_stream = fopen("temp.txt", "w");int written = fwprintf_s(write_stream, L"You_scored_%f/%d_in_your_test.%c", j, i, n);fclose(write_stream);wprintf(L"Characters Written: %d\n", written);FILE * read_stream = fopen("temp.txt", "r");wchar_t arr[100];fwscanf(read_stream, L"%s", arr);fclose(read_stream);wprintf(L"%s", arr);return 0;}
Output:
You_scored_5.500000/10_in_your_test
In the example above, the format string You_scored_%f/%d_in_your_test.%c evaluates to You_scored_5.500000/10_in_your_test.\n, which is written into a file temp.txt using the fwprintf_s function. The code in line 14 onward prints the contents of the file temp.txt onto the screen.
Note: The function above is not supported by the GCC compiler, so you will get an
implicit declaration of function…error.
Use the following variant fwprintf to get the job done:
int fwprintf(FILE * stream, const wchar_t * format, ...);
All parameters and return values are the same as fwprintf_s function.
Free Resources