【发布时间】:2013-10-22 10:57:20
【问题描述】:
我需要将整数打印到文件中。我将在下面发布代码。此代码的输出是一个空文件,它不包含整数。
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);
【问题讨论】:
-
您的代码运行良好。仅用于
out1 = fopen("outfile", "w");和int paste=1000;的测试检查。 -
您确定您查看的是正确的输出文件吗?
-
显示其余代码,尤其是定义和初始化 outfile 和 noptr 的地方。
-
是的,输出文件是由程序自己创建的。我一直在编译之前将其删除。每次都会创建文件,但它是空的。
-
printf("noptr->no = %d\n",noptr->no);打印正确的输出,所以我认为问题与它无关。变量 outfile 的值为“out”,程序创建“out”,但它是空的。