为什么命令提示符在我开始之前显示数字?(Why does command prompt show numbers before I begin?)
命令提示符在程序开始之前显示数字。 为什么? 给出了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; }The command prompt shows numbers before program begins. Why? 2687688 is given but the numbers won't write to file?
#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);同
scanf("%d",&n);printf将格式指向的C字符串写入标准输出(stdout)
scanf从stdin读取数据
在您的代码中,您打印n,即未初始化,在"Enter numbers to be stored in file"字符串后打印出一个随机数。
Substitute
printf("%d",&n);with
scanf("%d",&n);printf Writes the C string pointed by format to the standard output (stdout)
scanf Reads data from stdin
In your code you are printing n, that is not initialized, that a random number is printed out after "Enter numbers to be stored in file" string.
更多推荐
发布评论