实验发现: 下面的代码只能读以回车或空格分割的内容 ,“逗号” 也认为是要读的内容
C++例程:
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
intnum;
intsum;
ifstreamfilein("sumit.in");
if(filein.fail())
{
cerr<< "Could not read from file sumit.in " << endl;
}
else
{
sum= 0;
while(!filein.eof())
{
filein>> num;
if(num > 0)
{
sum+= num;
}
}
cout<< "The sum of the integers is " << sum << endl;
}
return0;
}
C例程:
#include <stdio.h>
#define INFILENAME "sumit.in"
int main (int argc, char **argv)
{
FILE *fp = fopen(INFILENAME,"r");
intsum = 0;
int i;
if(fp)
{
while(1==fscanf(fp, "%d", &i))
{
sum+= i > 0 ? i : 0;
}
fclose(fp);
printf("Thesum of the integers is %d \n",sum);
}
else
{
printf("Couldnot read from file sumit.in \n");
return(22);
}
return0;
}