Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to read and 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[32] = L"abc";

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

    fclose(writefp);

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

    
    FILE* readfp = fopen("d:\\data.txt", "r");

    n = 0;
    pi = 0.0;
    wch = ' ';
    wstr[0] = L"";

    fwscanf(readfp, L"%d", &n);
    fwscanf(readfp, L"%lf", &pi);
    fwscanf(readfp, L"%c", &wch);
    fwscanf(readfp, L"%s", wstr);

    fclose(readfp);
    
    wprintf(L"%d %lf %c %s", n, pi, wch, wstr);

    return 0;
}



/*
run:

total chars written = 15
4353 0.140000 A abc

*/

 



answered Apr 27, 2024 by avibootz

Related questions

1 answer 130 views
1 answer 130 views
1 answer 75 views
1 answer 139 views
1 answer 151 views
151 views asked Dec 30, 2020 by avibootz
1 answer 156 views
156 views asked Dec 29, 2020 by avibootz
...