注册 登录
编程论坛 C++教室

文件读入问题

zzt_428 发布于 2008-08-22 13:41, 622 次点击
这是我学习文件读入时写的一个程序,请大家帮个忙,为什么总是少读一个数字呢?多谢指点.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int SIZE = 60;
int main()
{
    char fileName[SIZE];
    ifstream inFile;
    
    cout << "Please enter the file name:";
    cin.getline(fileName,SIZE);
    inFile.open(fileName);
    if(!inFile.is_open())
    {
        cout << "Sorry! This file can't open ."
             << "Program terminating.";
        exit(EXIT_FAILURE);
    }
    double value;
    double sum=0;
    int count=0;

    inFile >> value;
    while(inFile.good())
    {
        ++count;
        sum += value;
        inFile >> value;
    }
    if(inFile.eof())
        cout << "It's the end of the file.\n";
    else if(inFile.fail())
        cout << "Input terminated by data mismatch.\n";
    else
        cout << "Input terminated by unknown reason.\n";
    
    if(0 == count)
        cout << "No data processed.\n";
    else
    {
        cout << "Items read:" << count << endl;
        cout << "Sum:" << sum << endl;
        cout << "Average:" << sum/count << endl;
    }
    inFile.close();

    
    return 0;
}
4 回复
#2
zzt_4282008-08-22 13:42
补充
文件中数据如下:
18 19 18.5 13.5 14
16 19.5 20 18 12 18.5
#3
zerocn2008-08-22 17:25
程序代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int SIZE = 60;
int main()
{
    char fileName[SIZE];
    ifstream inFile;
   
    cout << "Please enter the file name:";
    cin.getline(fileName,SIZE);
    inFile.open(fileName);
    if(!inFile.is_open())
    {
        cout << "Sorry! This file can't open ."
             << "Program terminating.";
        exit(EXIT_FAILURE);
    }
    double value;
    double sum=0;
    int count=0;

    //inFile>>value;这个要去掉,因为运行了这个后,文件指针已经指向了后面,你试着只分析3个数据就会明白的
    while(inFile.good())
    {   inFile >> value;
        ++count;
        sum += value;
        
    }
    if(inFile.eof())
        cout << "It's the end of the file.\n";
    else if(inFile.fail())
        cout << "Input terminated by data mismatch.\n";
    else
        cout << "Input terminated by unknown reason.\n";
   
    if(0 == count)
        cout << "No data processed.\n";
    else
    {
        cout << "Items read:" << count << endl;
        cout << "Sum:" << sum << endl;
        cout << "Average:" << sum/count << endl;
    }
    inFile.close();

   
    return 0;
}
#4
zzt_4282008-08-22 19:25
总结
上面说的对,但是也可以不把inFile >> value 去掉!
可以把
while(inFile.good())
{   
    inFile >> value;  //这一行放到
    ++count;
    sum += value;     
    //这里来
         
}
#5
xqls_xqls2008-08-22 20:51
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int SIZE = 60;
int main()
{
    char fileName[SIZE];
    ifstream inFile;
   
    cout << "Please enter the file name:";
    cin.getline(fileName,SIZE);
    inFile.open(fileName);
    if(!inFile.is_open())
    {
        cout<< "Sorry! This file can't open ."
            << "Program terminating.";
        exit(EXIT_FAILURE);
    }
    double value;
    double sum=0;
    int count=0;
    
    
    while(inFile>>value) //改成这样就可以了
    {
        ++count;
        sum += value;
        //inFile>>value;
        
    }
    if(inFile.eof())
        cout << "It's the end of the file.\n";
    else if(inFile.fail())
        cout << "Input terminated by data mismatch.\n";
    else
        cout << "Input terminated by unknown reason.\n";
   
    if(count==0)
        cout << "No data processed.\n";
    else
    {
        cout << "Items read:" << count << endl;
        cout << "Sum:" << sum << endl;
        cout << "Average:" << sum/count << endl;
    }
    inFile.close();
    system("pause");
    return 0;
}
1