文件改错、

[复制链接]
查看11 | 回复1 | 2010-6-3 14:48:47 | 显示全部楼层 |阅读模式
#include
#include
int main()
{
FILE *fp;
int n,sum = 0;
if((fp = fopen("n.txt","r")) == NULL)
{
printf("Cannot open the file!");
exit(0);
}
while(fscanf(fp,"%d",&n) == EOF)
{
sum = sum + n;

fscanf(fp,"%d ",&n);
}
fprintf(fp,"%d ",sum);
fclose(fp);
return 0;
}
将文件中的数据累加的程序,无法实现其功能,请高手帮忙改错!!!
谢谢了哈
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

回复

使用道具 举报

千问 | 2010-6-3 14:48:47 | 显示全部楼层
修改如下://---------------------------------------------------------------------------#include #include int main(){ FILE *fp; int n,sum = 0; if((fp = fopen("n.txt","r+")) == NULL) /*注意这里,如果没有加号,则只能读取,不能写入*/ {printf("Cannot open the file!");exit(0); } while(fscanf(fp,"%d",&n) != EO
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行