将整数写入c中的文件

时间:2013-10-22 10:57:21

标签: c file printf

我需要将整数打印到文件中。我将在下面发布代码。此代码的输出是一个空文件,它不包含整数。

FILE *out1;
out1 = fopen(outfile, "w");
if( out1 == NULL )
   {
      perror("Error while opening the output file.\n");
      exit(EXIT_FAILURE);
   }
int paste = noptr->no;
printf("message received %d\n", paste);
fprintf(out1,"%d\n", paste);
printf("printed!\n");
fclose(out1);

2 个答案:

答案 0 :(得分:0)

您的代码适用于我。也许您的int为0并且您在文本编辑器中打开文件。检查输出文件大小。

答案 1 :(得分:-1)

如果您愿意使用 name outfile写入文件,

out1 = fopen("outfile", "w");