| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 215 人关注过本帖
标题:文件读入问题
收藏  订阅  推荐  打印 
zzt_428
Rank: 2
来自:南京师范大学
等级:注册会员
威望:1
帖子:148
积分:1656
注册:2008-7-6
文件读入问题

这是我学习文件读入时写的一个程序,请大家帮个忙,为什么总是少读一个数字呢?多谢指点.

#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;
}
搜索更多相关主题的帖子: 大撒  
2008-8-22 13:41
zzt_428
Rank: 2
来自:南京师范大学
等级:注册会员
威望:1
帖子:148
积分:1656
注册:2008-7-6
补充

文件中数据如下:
18 19 18.5 13.5 14
16 19.5 20 18 12 18.5
2008-8-22 13:42
zerocn
Rank: 2
等级:注册会员
帖子:124
积分:1458
注册:2006-4-11


程序代码:
#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;

    [color=Red]//inFile>>value;这个要去掉[/color],因为运行了这个后,文件指针已经指向了后面,你试着只分析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;
}
2008-8-22 17:25
zzt_428
Rank: 2
来自:南京师范大学
等级:注册会员
威望:1
帖子:148
积分:1656
注册:2008-7-6
总结

上面说的对,但是也可以不把inFile >> value 去掉!
可以把
while(inFile.good())
{   
    inFile >> value;  //这一行放到
    ++count;
    sum += value;     
    //这里来
         
}
2008-8-22 19:25
xqls_xqls
Rank: 1
等级:新手上路
帖子:2
积分:200
注册:2008-8-22

#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;
}
2008-8-22 20:51
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.049244 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved