C中的二进制IO中发生了什么?

C中的二进制IO中发生了什么?,c,binary,C,Binary,我想这也适用于许多其他语言 我这里有这段代码,我想知道实际发生了什么,以及如何解释写入文件的内容 #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { //Initialize a variable and load it with input using scanf char write[100] = "Write this."; FIL

我想这也适用于许多其他语言

我这里有这段代码,我想知道实际发生了什么,以及如何解释写入文件的内容

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () 
{
    //Initialize a variable and load it with input using scanf
    char write[100] = "Write this.";

    FILE *fp = fopen("./binFile.txt", "w+");
    fwrite(write, sizeof(write[0]), sizeof(write)/sizeof(write[0]), fp);
    fclose(fp);
}

但我很难看清这里到底发生了什么。此文本如何分解为二进制?

您正在将整个字符数组写入文件,字符数组被初始化为12个字符,后跟所有零值。这就是为什么你在字符串后面看到所有的零,也许这让你的系统认为它是一个二进制文件。因此,如果要编写字符串,请使用以下命令:

fwrite(write, sizeof(write[0]), strlen(write), fp);

您正在使用显示文件中字符十六进制代码的程序打开文本文件
57
表示
W
72
表示
r
,等等。如果您在文本编辑器中打开文件,您会看到
编写此文件。
之后是编辑器,但该编辑器会依次显示null?你从来没有听说过ASCII?你希望有什么输出吗?@M.M我知道十六进制代码57=87 ASCII=W。明白了。谢谢为什么是这样的集群(57726974..而不是57726974)您使用的编辑器认为您希望看到这样的集群。您可以通过编辑器的配置选项更改此设置
fwrite(write, sizeof(write[0]), strlen(write), fp);