请教文件流的编程题

fulimei1002 2008-12-24 04:24:21
通过输入一个文件名(文件可能存在,也可能不存在)
1.显示文件内容到控制台;
2.从控制台输入任何内容追加到文件末尾;
3.询问是否继续追加内容
...全文
112 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangshuiling 2008-12-25
  • 打赏
  • 举报
回复
加入清流缓冲区指令:cin.sync();cin.clear();
smdszgzh 2008-12-25
  • 打赏
  • 举报
回复
有个地方不太清楚

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

fstream fs("test.txt");
cout<<fs.rdbuf()<<endl;//1.显示文件内容到控制台;
string line;
do
{
while(getline(cin,line))
fs<<line;
cout<<"是否继续:";//请问这句话什么时候输出?按ctrl+z的话会不断的输出这句话
char choice;
cin>>choice;
if(choice=='n')
break;
}while(true);
return 0;
}
xusleep 2008-12-25
  • 打赏
  • 举报
回复
你没有文件test.txt呀,建立这样一个文本文件随便写点什么,放在工程目录下即可
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

fstream fs("test.txt");
if (!fs.is_open()){
cout << "打开文件失败" << endl;
return 1;
}
cout<<fs.rdbuf();//1.显示文件内容到控制台;
string line;
do
{
while(getline(cin,line))
fs<<line;

cout<<"是否继续:";
char choice;
cin>>choice;
if(choice=='n')
break;
}while(true);

return 0;
}
fulimei1002 2008-12-25
  • 打赏
  • 举报
回复
运行了一下 不行啊 初学者不懂这些 呵呵
太乙 2008-12-24
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

fstream fs("test.txt");
if (!fs.is_open())return 1;
cout<<fs.rdbuf();//1.显示文件内容到控制台;
string line;
do
{
while(getline(cin,line))
fs<<line;

cout<<"是否继续:";
char choice;
cin>>choice;
if(choice=='n')
break;
}while(true);

return 0;
}
太乙 2008-12-24
  • 打赏
  • 举报
回复
随手写的,没测试,你试试~~
太乙 2008-12-24
  • 打赏
  • 举报
回复
……

1.显示文件内容到控制台;
2.从控制台输入任何内容追加到文件末尾;
3.询问是否继续追加内容


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

fstream fs("test.txt");
cout<<fs.rdbuf();//1.显示文件内容到控制台;
string line;
do
{
while(getline(cin,line))
fs<<line;

cout<<"是否继续:";
char choice;
cin>>choice;
if(choice=='n')
break;
}while(true)



return 0;
}
太乙 2008-12-24
  • 打赏
  • 举报
回复
……

1.显示文件内容到控制台;
2.从控制台输入任何内容追加到文件末尾;
3.询问是否继续追加内容


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

fstream fs("test.txt");
cout<<fs.rdbuf();//1.显示文件内容到控制台;
string line;
do
{
while(getline(cin,line))
fs<<line;

cout<<"是否继续:";
char choice;
cin>>choice;
if(choice=='n')
break;
}while(true)



return 0;
}
xiaoyisnail 2008-12-24
  • 打赏
  • 举报
回复
很基础的问题,随便找本c++的书上都有例子啊
ckt 2008-12-24
  • 打赏
  • 举报
回复
...

65,211

社区成员

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

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