| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 540 人关注过本帖
标题:ifstream问题
只看楼主 加入收藏
shanyuxiyun
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-5-14
收藏
 问题点数:0 回复次数:3 
ifstream问题
#include<iostream>
#include<fstream>
#include<cassert>
#include<cstdlib>
using namespace std ;

int main()
{
    ofstream out ;
    out.open("D:\\data.dat",ios_base::out);
    assert(out);
   
    int i ;
    const int size = 10 ;
    int data ;
    for(i = 0 ; i<size ;i++)
    {
          data = rand() ;
          out<<data<<"\t";
    }
    out<<endl;
    out.close() ;
   
    ifstream in("D:\\data.dat");
    assert(in);
    int cnt = 0 ;
    while(!in.eof())
    {
    in>>data ;
    cnt ++ ;
    }
    cout<<"cnt="<<cnt<<endl;
    system("pause");
    return 0 ;
   
    }
为什么cnt是11?
而不是10!
搜索更多相关主题的帖子: ifstream 
2010-05-15 20:16
DAVID_LIU
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-14
收藏
得分:0 
///////////////
 while(!in.eof())
    {
    in>>data ;
    cnt ++ ;
    }
////////////////
改成下面就行了
while(!in.eof())
    {
        cnt ++ ;
      in>>data ;

    }

谦虚做人
2010-05-18 17:03
猎豹
Rank: 1
来 自:河南
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-4-10
收藏
得分:0 
好像还不行
2010-05-20 13:12
yc2575757
Rank: 7Rank: 7Rank: 7
来 自:北京
等 级:黑侠
威 望:1
帖 子:113
专家分:522
注 册:2010-5-7
收藏
得分:0 
#include<iostream>
#include<fstream>
#include<cassert>
#include<cstdlib>
using namespace std ;

int main()
{
    ofstream out ;
    out.open("D:\\data.dat",ios_base::out);
    assert(out);
   
    int i ;
    const int size = 10 ;
    int data ;
    for(i = 0 ; i<size ;i++)
    {
          data = rand() ;
          if (i!=size-1)
          {
              out<<data<<"\t";
          }

    }
    out<<endl;
    out.close() ;
   
    ifstream in("D:\\data.dat");
    assert(in);
    int cnt = 0 ;
    while(!in.eof())
    {
    in>>data ;
    cnt ++ ;
    }
    cout<<"cnt="<<cnt<<endl;
    system("pause");
    return 0 ;
   
}
2010-05-20 16:14
快速回复:ifstream问题
数据加载中...
 
   



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

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