Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用c语言显示写入文件的二进制结果_C_Binaryfiles - Fatal编程技术网

如何用c语言显示写入文件的二进制结果

如何用c语言显示写入文件的二进制结果,c,binaryfiles,C,Binaryfiles,我需要用C语言在文件中写入结构地址的值 void func_1 ( struct_1 *st) 以及位于.h文件中的类型struct_1 struct struct_1 { target_ulong a; target_ulong b; uint32_t c; uint16_t size; } 我用这个代码在file.txt文件中写入 FILE *fp; fp=fopen("/path/to//doc.txt", "wb"); int

我需要用C语言在文件中写入结构地址的值

void func_1 ( struct_1 *st)
以及位于.h文件中的类型struct_1

struct struct_1 {
      target_ulong a;   
      target_ulong b; 
      uint32_t c; 
      uint16_t size;
}
我用这个代码在file.txt文件中写入

FILE *fp;
fp=fopen("/path/to//doc.txt", "wb");
int x=sizeof((void *)st);
fwrite (st, x, 100, fp);
fclose(fp);
当我打开doc.txt时,结果是这样的


似乎文件的格式无法用gedit打开。你能推荐另一种工具来打开它吗。

问题在于编辑器默认情况下搜索ASCII码并显示字符。在这种情况下,我打开文件是为了以二进制模式写入,所以出现了问题


我使用了一个十六进制编辑器,可以打开并显示二进制文件

地址点到内存位置。它们不指向文件中的位置。您希望x中的值是多少?如果您想要使用sizeof*st.As@Chris write的结构的大小,sizeofvoid*x将返回一个空指针的大小。我不想在一篇看起来像新手文章的文章中指出Advanced Linux C,但这是可能的。等待-您正在尝试将st的地址或st的内容写入您的文件吗?如果在以后尝试读取文件时,前者没有意义,则可以使用fwrite&st,sizeof st,1,fp;。如果是后者,则使用fwrite st,sizeof*st,1,fp;。