c中的文件处理没有产生所需的结果

我是文件处理的新手,当我尝试从键盘读取数据到文件并在屏幕上输出该文件的内容时,我没有得到所需的结果,下面的代码

/* get data from the keyboared till the end of file and write it to the file named "input" agian read the data from this file on to the screen*/ #include  int main() { FILE *fp; char c; printf("enter the data from the keyboared\n"); fp=fopen("input.txt","w"); while((c=getchar()!=EOF)) { putc(c,fp); } fclose(fp); printf("reading the data from the file named input\n"); fopen("input.txt","r"); while((c=getc(fp))!=EOF) { printf("%c",c); } fclose(fp); return 0; } 

我得到像这样的输出?

还有一种方法,以便我可以找到硬盘上创建此文件的位置?

首先,由于优先权,这是错误的。

 while((c=getchar()!=EOF)) ^ 

您将持续存储角色和EOF之间的比较,而不是存储角色。 所以你将连续存储一长串1

试试这个:

 while((c=getchar())!=EOF) ^ 

第二个getcgetchar返回int 。 所以ch应该是int ,而不是char 。 使用char可能意味着循环永远不会在某些系统上终止。

这条线:

 fopen("input.txt","r"); 

显然是错的。 似乎你想要:

 fp = fopen("input.txt","r"); 

代替。