Converter C

Hi. My task is to write a program that converts CP437 text symbols into UTF-8. I've done something but it's output not exactly what I need. The program changes text file encoding from ANSI to UTF-8 but the same symbols in the output text are not changed (and CP437 to UTF-8 gives a blank text file at all). How to see these changes? Maybe it's rather to print the whole output text into the console than save it in file?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* src = fopen("input.txt", "rb");
    if(!src) return 0;
 
    FILE* dst = fopen("output.txt", "wb");
    if(!dst) return 0;
 
    fseek(src, 0, SEEK_END);
    long filesize = ftell(src);
    char *ansi = malloc(filesize);
 
    fseek(src, 0, SEEK_SET);
    fread(ansi, 1, filesize, src);
 
    int codepage = CP_ACP;
    int u16size = MultiByteToWideChar(codepage, 0, ansi, filesize, NULL, 0);
    wchar_t *u16 = malloc(u16size * sizeof(wchar_t));
    MultiByteToWideChar(codepage, 0, ansi, filesize, u16, u16size);
 
    int u8size = WideCharToMultiByte(CP_UTF8, 0, u16, u16size, NULL, 0, NULL, FALSE);
    char *u8 = malloc(u8size);
    WideCharToMultiByte(CP_UTF8, 0, u16, u16size, u8, u8size, NULL, FALSE);
 
    fwrite(u8, 1, u8size, dst);
    return 0;
}
Last edited on
look at it in a hex editor to see if the converted file (the one that has data) is actually correct? Can you actually tell any difference if you open it in a smart editor? Maybe open it in a dumb editor that isnt multibyte aware?

print to console and use > to write to a file works great for some programs. You could try it but I suspect that isnt the issue.
seems like write would be wb+, but that isn't critical.

The blank one we can fix after getting the other one working.

Topic archived. No new replies allowed.