标题:.txt读取中的问题
取消只看楼主
y605302737
Rank: 1
等 级:新手上路
帖 子:32
专家分:9
注 册:2013-2-2
结帖率:81.82%
已结贴  问题点数:10 回复次数:1 
.txt读取中的问题
在读取.txt时遇到一个问题
txt文件是:
y x z
yyyy yyyyy
xxxxxxxxxx
zzzz zzzzz
读取程序
程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

int main()
{
    using namespace std;
    ifstream fin;
    fin.open("name.txt");
    if(!fin.is_open())
    {
        cerr<<"cannot open"<<endl;
        exit(EXIT_FAILURE);
    }
    string item;
    int count=0;
    getline(fin,item,'\n');
    while(fin)
    {
        ++count;
        cout<<count<<item<<endl;
        getline(fin,item,'\n');
    }
    cout<<"over gamme"<<endl;
    fin.close();
    return 0;
}
输出结果
1y x z
2yyyy yyyyy
3xxxxxxxxxx
4zzzz zzzzz
over gamme
不过将while(fin)改为while(fin.good())后的结果为:
1y x z
2yyyy yyyyy
3xxxxxxxxxx
over gamme
这是为什么,求高手指教! 谢谢!

搜索更多相关主题的帖子: zzzzz yyyyy 
2013-02-18 21:31
y605302737
Rank: 1
等 级:新手上路
帖 子:32
专家分:9
注 册:2013-2-2
得分:0 
回复 4楼 rjsp
弱弱的问一句:exit有什么问题 ,我是菜鸟!
2013-02-20 17:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-398757-1-1.html




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

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