对同一个文件进行读写

APIandMFC 2008-11-25 12:47:00

#include<string>
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
fstream tfile("yanxu.txt",ios::out|ios::in);
if(tfile.fail())
{
cout<<"Can't open the file"<<endl;
exit(0);
}
tfile<<"hello"<<endl;
string line;
while(getline(tfile,line))
{
cout<<line<<endl;
}
return 0;
}


帮忙看看 为什么不对啊?谢谢了~~
...全文
224 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyingwow99 2008-12-18
  • 打赏
  • 举报
回复
同问:
(1) c++ fstream 中的读指针 和写指针 不是分开的吗, 定位分别用seekg seekp, 获得位置分别用tellg tellp, 为什么 tfile<<"hello"<<endl 向文件写数据之后, 要用seekg来定位写指针的位置呢??
(2) 当 f.eof() 返回true后, 要不要clear(),不clear只重新seek,读写能正常否?

veloting 2008-11-25
  • 打赏
  • 举报
回复
#include<string>
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
fstream tfile("yanxu.txt",ios::out|ios::in);
if(tfile.fail())
{
cout<<"Can't open the file"<<endl;
exit(0);
}
tfile<<"hello"<<endl;
tfile.seekg(0,ios::beg);//add this
string line;
while(getline(tfile,line))
{
cout<<line<<endl;
}
return 0;
}

写完文件之后它的指向的是文件的最后,当然输不出hello
sk19891117 2008-11-25
  • 打赏
  • 举报
回复
报错语句都不给看哦
lann64 2008-11-25
  • 打赏
  • 举报
回复
文件指针问题,需要重定位。
cba_v 2008-11-25
  • 打赏
  • 举报
回复
up
p89787259 2008-11-25
  • 打赏
  • 举报
回复
xue xi le
p89787259 2008-11-25
  • 打赏
  • 举报
回复
xue xi le
cnmghi 2008-11-25
  • 打赏
  • 举报
回复
楼上两位都对了,我就不掺和了,呵呵
veloting 2008-11-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ztz0223 的回复:]
C/C++ code#include<string>
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
fstream tfile("readme.txt",ios::out|ios::in);
if(tfile.fail())
{
cout<<"Can't open the file"<<endl;
exit(0);
}
tfile<<"hello"<<endl;
string line;
///////////添加就可以看到,文件已经失败了!
if(tfile.fail())
{
cout<<"Can't o…
[/Quote]
测试结果表明写入完成之后文件没有失效
  1 #include<string>
2 #include<fstream>
3 #include<iostream>
4 using namespace std;
5 int main()
6 {
7 fstream tfile("yanxu.txt",ios::out|ios::in);
8 if(tfile.fail())
9 {
10 cout<<"Can't open the file"<<endl;
11 exit(0);
12 }
13 tfile<<"hello"<<endl;
14 tfile<<"world"<<endl;
15 tfile.seekg(0,ios::beg);
16 string line;
17 while(getline(tfile,line))
18 {
19 cout<<line<<endl;
20 }
21 return 0;
22 }

如果失效的话world输不出来,但是结果是hello和world都可以输出
就呆在云上 2008-11-25
  • 打赏
  • 举报
回复
#include<string>
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
fstream tfile("readme.txt",ios::out|ios::in);
if(tfile.fail())
{
cout<<"Can't open the file"<<endl;
exit(0);
}
tfile<<"hello"<<endl;
string line;
///////////添加就可以看到,文件已经失败了!
if(tfile.fail())
{
cout<<"Can't open the file"<<endl;
tfile.close(); //关闭然后再打开
tfile.open("readme.txt");
}

tfile.seekg(0,ios::beg);
while(getline(tfile,line))
{
cout<<line<<endl;//一定要有endl
}
return 0;
}


写入完成之后文件就失效了,先关闭然后再打开就是了,还有重新定位到文件的开头
ChamPagneZ 2008-11-25
  • 打赏
  • 举报
回复

#include<string>
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
fstream tfile("yanxu.txt",ios::out|ios::in|ios::app); // here
if(tfile.fail())
{
cout<<"Can't open the file"<<endl;
exit(0);
}
tfile<<"hello"<<endl;
tfile.seekg(0,ios::beg);//add this
string line;
while(getline(tfile,line))
{
cout<<line<<endl;
}
return 0;
}

64,647

社区成员

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

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