
关于用流类库编写一个简单的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;
}
表示没有思路。。 展开
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);
}
}
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询