【发布时间】:2015-08-23 21:20:51
【问题描述】:
命令提示符在程序开始前显示数字。为什么? 2687688 已给出
但数字不会写入文件?
#include <stdio.h>
#include <conio.h>
int main(void){
FILE*nfPtr;
int n;
if ((nfPtr=fopen("c:\\Users\\raphaeljones\\Desktop\\newfile.dat","w"))==NULL)
{
printf ("Sorry! The file cannot be opened\n");
}
else
{//else 1 begin
printf("Enter numbers to be stored in file\n");
printf("%d",&n);
while (!feof(stdin)){
fprintf(nfPtr,"%d",n);
scanf("%d",&n);
}
}//else 1 ends
fclose(nfPtr);
getch();
return 0;
}
【问题讨论】:
-
因为你有
printf("%d",&n);- 你的编译器不会发出警告吗? -
另外,将
fclose(nfPtr)移动到else块的主体中。因为fclose(NULL)是UB。