与输入输出流操作相关的类

标准流对象

输出重定向
#include <iostream>
using namespace std;
void main() {
int x, y;
cin >> x >> y;
freopen("test.txt", "w", stdout); // 将标准输入输出重定向到test.txt文件
if(y == 0) // 如果除数是零,则在屏幕上打印错误信息
cerr << "error." << endl; // 否则将结果写入文件中
else
cout << x / y;
}
- 重定向后,我们可以将调试信息打印到屏幕上,而有用的信息写入文件,而不会将调试信息也写入文件
输入重定向
#include <iostream>
using namespace std;
void main() {
double f; int n;
freopen("t.txt", "r", stdin);
cin >> f >> n;
cout << f << "," << n << endl;
}
判断输入流结束
istream类的成员函数


#include <iostream>
using namespace std;
void main()
{
int x;
char buf[100];
cin >> x;
cin.getline(buf, 90);
cout << buf << endl;
}
