无法使用fwrite将int写入文件 [英] Can't write int to file using fwrite

查看:92
本文介绍了无法使用fwrite将int写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to format my keylog output so it shows time:

        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);
        }

但是我没有在文件中写入"[DLE NUL]",而DLENUL是问号.

but instead of readable number I get "[ DLE NUL ] " written in my file, where DLENUL is question mark.

我如何写一个可读的数字?

How do I make it to write a readable number?

推荐答案

fprintf 用作其他建议.

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

Reason:
fwrite is generally used to write in binary files to write blocks of same type of data.

您要写入的数据看起来像一个字符串,可以使用具有以下语法的 fprintf 将完​​整的数据写入文件中.

The data you are writing looks like a character string, you can use fprintf with following syntax to write your complete data in the file.

 fprintf(keylog, "[%d] ", prevH);

似乎您正在写宽字符(使用 wchar 时).您可以相应地使用不同的格式说明符.

It seems you are writing wide characters (as you use wchar). You can use different format specifiers accordingly.

这篇关于无法使用fwrite将int写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆