gpt4 book ai didi

c - 为什么命令提示符在开始之前显示数字?

转载 作者:行者123 更新时间:2023-11-30 18:39:11 25 4
gpt4 key购买 nike

命令提示符在程序开始前显示数字。为什么?给出了 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);

绝对错误并调用 undefined behaviour 。。也许你的意思是

 scanf("%d",&n);

扫描号码。

也就是说,请参阅 why you should refrain from using !feof(file)

关于c - 为什么命令提示符在开始之前显示数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30725577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com