当前位置: 代码迷 >> C语言 >> 请教一个关于文件输出的问题
  详细解决方案

请教一个关于文件输出的问题

热度:137   发布时间:2008-03-28 12:06:11.0
请教一个关于文件输出的问题
#include <stdio.h>
struct mystruct
{
  int i;
  char ch;
};

int main(void)
{
   FILE *stream;
   struct mystruct s;

   if ((stream = fopen("TEST.txt", "wb")) == NULL) /* open file TEST.$$$ */
   {
      fprintf(stderr, "Cannot open output file.\n");
      return 1;
   }
   s.i = 120;
   s.ch = 'A';
   fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */
   fclose(stream); /* close file */
   return 0;
}
文件打开后,发现里面的内容:x A
也就是说s.i的内容120 以ascii码的形式输出到文件中去了

除了以字符串的方式输出之外,还能怎么解决?才能让整型变量的内容直接输出到文件中去呢?
谢谢
搜索更多相关的解决方案: stream  文件  file  struct  

----------------解决方案--------------------------------------------------------
fwrite以二进制形式把一整数写入TEST.txt,此为文本文件,以文本编辑器打开,就会出现这种现象。
----------------解决方案--------------------------------------------------------
  相关解决方案