64,647
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}
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 }
#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;
}
#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;
}