fstream的用法问题!

wpalhm 2007-05-19 11:13:37

#include <fstream>
#include <iostream>
using namespace std;
int main()
{

ifstream inifile;
inifile.open("e:/123.txt");
if(!inifile)
{
cerr<<"file is not open"<<endl;
return -1;
}
while(inifile>>"hello")
cout<<"ok"<<endl;

inifile.close();
inifile.clear();
return 0;
}
为什么会在执行时提示;Text256.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。
...全文
231 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wpalhm 2007-05-19
  • 打赏
  • 举报
回复
都还是有问题啊!我的目的是想在e盘下的123.txt文件中写入数据。可以都不能成功
KSlinfeng 2007-05-19
  • 打赏
  • 举报
回复
看兄台的意思好像是
(Dev-cpp)
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
ifstream inifile;
inifile.open("c://123.txt");
if(!inifile)
{
cerr<<"file is not open"<<endl;
}
else
{
string str("hello");
string in;
while((inifile>>in)&&(in==str)) //从文件读入并判断读入内容
{
cout<<str<<endl
<<"ok"<<endl;
}
}
inifile.close();
inifile.clear();
system("PAUSE");
return EXIT_SUCCESS;
}

CHROX 2007-05-19
  • 打赏
  • 举报
回复
while(inifile>>"hello") //出错
cout<<"ok"<<endl;
你是不是想这样:
string str("hello");
string in;
while((inifile>>str)&&(in==str)){
cout<<"ok"<<endl;
}
iu_81 2007-05-19
  • 打赏
  • 举报
回复
inifile.open("e://123.txt");
KSlinfeng 2007-05-19
  • 打赏
  • 举报
回复
上面的代码,我没有终止while(inifile<<str)
无限循环 :)
KSlinfeng 2007-05-19
  • 打赏
  • 举报
回复
你上面代码的意思就是读入如果是写入的话
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
ofstream inifile;
inifile.open("c://123.txt");
if(!inifile)
{
cerr<<"file is not open"<<endl;
}
else
{
string str("hello");
while(inifile<<str) //文件写入
{
cout<<str<<endl
<<"ok"<<endl;
}
}
inifile.close();
inifile.clear();
system("PAUSE");
return EXIT_SUCCESS;
}
wpalhm 2007-05-19
  • 打赏
  • 举报
回复
谢谢大侠们。是小弟理解错误。现在懂了!!
yuyunliuhen 2007-05-19
  • 打赏
  • 举报
回复
mark

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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