关于用流类库编写一个简单的C++程序

1、编写程序,将“Helloworld!”字符串输出到“hello.txt”文本文件中。2、以第1题中生成的“hello.txt”文件作为输入文件,编写程序,从屏幕上输出... 1、编写程序,将“Hello world!”字符串输出到“hello.txt”文本文件中。

2、以第1题中生成的“hello.txt”文件作为输入文件,编写程序,从屏幕上输出“hello.txt”文件中的内容。

我第一个程序是这样写的:
#include<iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
ofstream fout("hello.txt");
if(!fout) {
cerr << "打开文件错误!" << endl;
exit(1);
}
fout << "Hello world!"<< endl;
fout.close();

ifstream fin("hello.txt");
if(!fin) {
cerr << "打开文件错误!" << endl;
exit(1);
}
string str;
fin >> str; cout << str << " ";

fin >> str; cout << str << endl;
return 0;
}
第一个怎么改可以不在屏幕上输出呢?

第二个老师说可以参考

#include <iostream>
using namespace std;

int main() {
char ch;
while((ch=cin.get())!=EOF)
cout.put(ch);
return 0;
}

表示没有思路。。
展开
 我来答
匿名用户
2012-06-15
展开全部
第一个
cout << str << " ";
cout << str << endl;
把这两句去掉,就可以实现不在屏幕输出。

第二个
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream fin( "hello.txt" );

if ( !fin )
{
cerr << "打开文件错误!" << endl;
exit(1);
}

char ch;
while( ( ch = fin.get() ) != EOF )
{
cout.put(ch);
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式