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,971 questions

51,913 answers

573 users

How to write and read Unicode to and from binary file with C Win32 API

1 Answer

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

#define SIZE 32

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    // Write unicode to binary file
    wchar_t unicode_buffer[SIZE] = L"ÖÉÁß";
    FILE* fp;
    
    if (fopen_s(&fp, "d:\\unicode_bin.dat", "wb") != 0) {
        wprintf(L"file write error\n");
        return 0;
    }
    fwrite(unicode_buffer, sizeof(wchar_t) * 4, 1, fp);
    fclose(fp);


    // Read unicode from binary file
    wchar_t read_unicode_buffer[SIZE] = L"";
    if (_wfopen_s(&fp, L"d:\\unicode_bin.dat", L"rb") != 0) {
        wprintf(L"file not found\n");
        return 0;
    }
    while (fread(read_unicode_buffer, sizeof(wchar_t) * 4, 1, fp)) {
        MessageBox(nullptr, read_unicode_buffer, TEXT(""), 0);
    }

    fclose(fp);

    return 0;
}



/*
run:

ÖÉÁß

*/

 



answered Aug 8, 2024 by avibootz
...