我正在使用 C 的 fwrite 函数将 3 个整数的数组写入文件,但是使用 gedit(使用 Unicode UTF-8)打开输出文件时,出现以下错误:
There was a problem opening the file. The file you opened has invalid characters. If you continue editing this file, you could corrupt the document.
这是相关的代码片段:
char* imageFile = "image.txt";
FILE* imageFilePtr = fopen(imageFile, "w");
int scores[3] = {69, 70, 71};
fwrite(scores, sizeof(int), sizeof(scores), imageFilePtr);
当我使用诸如“xxd”之类的十六进制阅读器时,我在终端中得到以下信息:
0000000: 4500 0000 4600 0000 4700 0000 7031 7108 E...F...G...p1q.
0000010: 0830 7108 2987 0408 2087 0408 0460 cebf .0q.)... ....`..
0000020: 0100 0000 0000 0000 0000 0000 0000 0000 ................
请记住,在我的环境中,sizeof(int) 是 4 个字节。因此,我可以看到十进制的 69、70 和 71 如何以十六进制的 45、46 和 47 形式打印到文件中,如 xxd 所示。但是,“4700 0000”之后的所有其他字节来自哪里?而且,为什么我不能用文本编辑器打开输出文件“image.txt”来查看显示十进制数字 69、70 和 71 的文件?