无法使用fwrite将int写入文件

leg*_*nar 2 c fwrite

我正在尝试格式化我的键盘输出,以便显示时间:

        t = time(0);
        now = localtime(&t);


        if(now->tm_min != prevM && now->tm_hour != prevH)
        {
            prevM = now->tm_min;
            prevH = now->tm_hour;

            fwrite("[", 1, sizeof(WCHAR), keylog);
            fwrite(&prevH, 1, sizeof(int), keylog);
            fwrite("]", 1, sizeof(WCHAR), keylog);
            fwrite(" ", 1, sizeof(WCHAR), keylog);
            fflush(keylog);
        }
Run Code Online (Sandbox Code Playgroud)

但是在我的文件中写了"[DLE NUL]"而不是可读数字,其中DLENUL是问号.

如何编写可读数字?

Don*_*ild 5

fprintf像其他人一样使用也在暗示.

原因:
fwrite通常用于写入二进制文件以写入相同类型数据的块.

您正在编写的数据看起来像一个字符串,您可以使用fprintf以下语法在文件中写入完整的数据.

 fprintf(keylog, "[%d] ", prevH);
Run Code Online (Sandbox Code Playgroud)

看起来你正在写宽字符(正如你所使用的那样wchar).您可以相应地使用不同的格式说明符.