| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 622 人关注过本帖
标题:文件读入问题
只看楼主 加入收藏
zzt_428
Rank: 2
来 自:南京师范大学
等 级:论坛游民
威 望:2
帖 子:243
专家分:22
注 册:2008-7-6
收藏
 问题点数:0 回复次数:4 
文件读入问题
这是我学习文件读入时写的一个程序,请大家帮个忙,为什么总是少读一个数字呢?多谢指点.

#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-08-22 13:41
zzt_428
Rank: 2
来 自:南京师范大学
等 级:论坛游民
威 望:2
帖 子:243
专家分:22
注 册:2008-7-6
收藏
得分:0 
补充
文件中数据如下:
18 19 18.5 13.5 14
16 19.5 20 18 12 18.5
2008-08-22 13:42
zerocn
Rank: 1
等 级:新手上路
帖 子:126
专家分:0
注 册:2006-4-11
收藏
得分:0 
程序代码:
#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;
}
2008-08-22 17:25
zzt_428
Rank: 2
来 自:南京师范大学
等 级:论坛游民
威 望:2
帖 子:243
专家分:22
注 册:2008-7-6
收藏
得分:0 
总结
上面说的对,但是也可以不把inFile >> value 去掉!
可以把
while(inFile.good())
{   
    inFile >> value;  //这一行放到
    ++count;
    sum += value;     
    //这里来
         
}
2008-08-22 19:25
xqls_xqls
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-8-22
收藏
得分:0 
#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-08-22 20:51
快速回复:文件读入问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015188 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved