2024年4月5日发(作者:)

eof:当程序读到

超过

文件结束位置时,表达式的值为true.

1

该程序:求出未知行的数列中,每行的总和。

#include

#include

#include

#include

using namespace std;

int main()

{

if(!in)

{

out<<"Can not open that file"<

("E:c++学习循环嵌套.txt");

("E:c++学习循环嵌套");

int i,count,num;

double sum;

ifstream in;

ofstream out;

}

return 1;

i=0;

in>>num;//这一部很关健,可以解决很多麻烦,例如下面的

程序

while(!()) //可以明白In 和eof命令的用法。

importang

}

out<<"The number of "<

out<<"The sum of "<

in>>num;

{

count=0;

sum=0;

while(num!=-999)

{

}

count++;

sum+=num;

in>>num;

();

();

return 0;

}

在同一个文档中,并且执行多次以后,直到覆盖上次的结果。

#include

#include

#include

#include

using namespace std;

int main()

{

ifstream in;

ofstream out;

double num,sum,aver;

int n=0;

sum=0;

("E:c++学习eof命令练习.txt");

("E:c++学习eof命令练习");

while(!())

//读到最后一个数据后,仍要读一次

{

in>>num;

sum+=num;

n++;

out<<"the number is : "<

out<<"the total of numbers is: "<

}

if(n==1)

{

out<<"NO numbers in that file"<

}

else

{

in>>num;

//读了最后一个数据两次,所以要减去最后一数据。

因为in已经指到最后一个数据,所以直接执行该命令。

sum-=num;

out<<"the total of numbers is: "<

aver=sum/(n-1);

out<<"the number is : "<

out<<"the average of numbers is : "<

}

return 0;

}