如何判断文件读取内容是否为空?

newman0708 2003-11-01 04:23:00
string filename("test.txt");
ifstream ifile(filename.c_str());
ostrstream os1;
os1<<ifile.rdbuf();

if(ifile.rdbuf()==NULL)//就是这里不会写了
cout<<"NULL"<<endl;
ifile.close ();
...全文
462 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
newman0708 2003-11-02
  • 打赏
  • 举报
回复
有办法去掉ifile.rdbuf ()中的结束符?
sunjx119 2003-11-02
  • 打赏
  • 举报
回复
ios::app

ifstream ifile(this->szFileName.c_str (),ios::app);

试试...
herrycsdn 2003-11-02
  • 打赏
  • 举报
回复
rdbuf ()应该是 包括文件的结束符的
newman0708 2003-11-02
  • 打赏
  • 举报
回复
ifile.rdbuf ()其中是不是包括了文件结束符?

如果有,怎么去掉它呢。
smalltalk 2003-11-02
  • 打赏
  • 举报
回复
实际上,你要完成的功能用三个语句就完成了!为什么那么复杂!
void appendString(string os){ //append text to the file
ofstream ofile(this->szFileName.c_str(), ios::app);
ofile << os;
ofile.close();
}

另有一点大家可以看到,CSDN上有些人真的很菜!
nmgpy 2003-11-01
  • 打赏
  • 举报
回复
ofstream ofile(this->szFileName.c_str ());
加上app的标志就可以了
newman0708 2003-11-01
  • 打赏
  • 举报
回复
sorry,是我没有讲清楚。

我主要是想实现追加的功能

void myFile::appendString(string os){ //append text to the file
ifstream ifile(this->szFileName.c_str ());
string str;
stringstream stream;
stream<<ifile.rdbuf ();<-------------------就是它(rdbuf())
stream>>str;
ifile.close ();
ofstream ofile(this->szFileName.c_str ());
ofile<<str<<endl<<os<<endl;
ofile.close ();
}

如果我五次追加“123456”,文件中内容应该是:
123456
123456
123456
123456
123456

但是,读取的内容只是:
123456
123456






leyt 2003-11-01
  • 打赏
  • 举报
回复
EOF 文件结尾
lemon520 2003-11-01
  • 打赏
  • 举报
回复
BOF 头
EOF 尾
ttlb 2003-11-01
  • 打赏
  • 举报
回复
if (ifile.eof())
{
// end of file;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧