正文

判断文件是否为空 2006-11-24 12:44:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jixian/20714.html

分享到:

//判断文件是否为空 //严重有问题啊,把写好的文件 换行就不行了。
#include   <iostream> 
#include <string>
#include   <fstream> 
#include <cstdlib>
using   namespace   std;  
int   main()  
{  
 ifstream   input("E:\\a.txt");  
    if(input)
 {
  string s;
  getline(input,s);
  if(s=="")
  {
   cout<<"空文件 "<<endl;
   input.close();
   ofstream output("E:\\a.txt");
   output<<"sadfd"<<endl;
   output.close();
  }
  else
  {
   ifstream output("E:\\a.txt");
   while(getline(output,s))
    cout<<s<<endl;
  }
  
 }  


}

 

网上找的类似的问题:

#include   <iostream>   
  #include   <fstream>   
  using   namespace   std;   
  int   main()   
  {   
  ifstream   input("a.txt");   
  if(input){   
  input.seekg(0,ios_base::end);   
  fstream::off_type   Len=input.tellg();   
  if(Len==0)   cout<<"Empty   file\n";   
  }   
  }

 

streamoff filelen( ifstream& file )  //判断文件是否为空 
{
    fstream::pos_type cur_pos = file.tellg();
    file.seekg( 0L, ios::end );
    fstream::pos_type end_pos = file.tellg();
    file.seekg( cur_pos, ios::beg );
    return end_pos;
}

len = filelen( infile );
if(0==len)//判断是否文件为空
{
...//写入
}
else
{
...//读出
}

 

阅读(5973) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册