[英]Reading data from file in console in c
Hi i have the following code 嗨,我有以下代码
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <conio.h>
int main()
{
FILE *fp;
char c=' ';
fp=fopen("E:\data.txt","w");
if(fp==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("Write data & to stop press '.' :");
while(c!='.')
{
c=getche();
fputc(c,fp);
}
fclose(fp);
printf("\nContents Read:");
fp=fopen("E:\data.txt","r");
while(!feof(fp));
printf("%c",getc(fp));
}
And when executing the above code , i have the following output 当执行上面的代码时,我有以下输出
Output: 输出:
Write data & to stop press '.' :writing data into the file.
Contents Read:
Output doesn't display the contents which i have inputted. 输出不显示我输入的内容。
Please help me where did i went wrong. 请帮助我我哪里做错了。
Your primary issue is here: 您的主要问题在这里:
while(!feof(fp));
The trailing semi-colon is the complete body of the loop, followed by a single call to printf. 尾部的分号是循环的完整主体,后面是对printf的单个调用。 However, Why is “while ( !feof (file) )” always wrong?
但是, 为什么“ while(!feof(file))”总是错误的? for other reasons.
由于其他原因。
There is typo here. 这里有错字。
while(!feof(fp));
take you to end of file. 带您到文件尾。
so just remove ;
所以删除
;
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.