How to use fwprintf_s to write wide characters to a file in C

1 Answer

0 votes
#include <stdio.h>
#include <wchar.h>

int main() {
    int n = 435;
    double pi = 3.14;
    wchar_t wch = 'A';
    wchar_t wstr[16] = L"abc\n";

    FILE* fp = fopen("d:\\data.txt", "w");
    int total_chars_written_to_file = fwprintf_s(fp, L"%d %lf %c %s", n, pi, wch, wstr);

    fclose(fp);

    wprintf(L"total chars written = %d\n", total_chars_written_to_file);

    return 0;
}



// data.txt
//
// 435 3.140000 A abc
// 



/*
run:

total chars written = 19

*/

 

 



answered Apr 27, 2024 by avibootz
edited Apr 27, 2024 by avibootz

Related questions

1 answer 161 views
1 answer 200 views
1 answer 188 views
1 answer 262 views
1 answer 190 views
...