管理输入包含两步
将流与输入去向的程序关联起来
将流与文件连接起来
管理输出也是
将流与输出去向连接起来
将流与文件连接起来
文件不一定是指文件 也有可能是其他的 比如打印机啦 扫描仪,摄像头啦之类的
管理缓冲区的类
cout <<运算符重载后能识别的所有的基本类型 也支持字符(包括数组)指针和string指针,其他类型要(void *)强转成指针即可输出
cout.put(‘i’) 输出i返回cout对象引用
write()
程序示例
#include <iostream>
#include <cstring>
int main()
{
using std::cout;
using std::endl;
const char* state1 = "Florida";
const char* state2 = "Kansas";
const char* state3 = "Euphoriz";
int len = std::strlen(state2);
cout << "Increasing loop index:\n";
int i;
for (i = 1; i<=len; i++)
{
cout.write(state2, i);
cout << endl;
}
cout << "Decreasing loop index:\n";
for (i=len; i > 0; i--)
{
cout.write(state2, i) << endl;
}
cout << "Exceeding string length:\n";
cout.write(state2, len + 5) << endl;
return 0;
}
程序运行结果
write()遇到空字符不会停止,依然会继续输出
缓冲区正常是512字节 满了自动输出刷新 但是不是每次都满的 哪等得了
所以 手动刷新输出缓冲区呗
flush(cout) 或者 cout << "fwfwfwe" << flush;
cout格式化
程序示例
#include <iostream>
int main()
{
using std::cout;
cout << "12345678901234567890\n";
char ch = 'K';
int t = 273;
cout << ch << ":\n";
cout << t << ":\n";
cout << -t << ":\n";
double f1 = 1.200;
cout << f1 << ":\n";
cout << (f1 + 1.0/ 9.0) << ":\n";
double f2 = 1.67E2;
cout << f2 << ":\n";
f2 += 1.0 / 9.0;
cout << f2 << ":\n";
cout << (f2 * 1.0e4) << ":\n";
double f3 = 2.3e-4;
cout << f3 << ":\n";
cout << f3 / 10 << ":\n";
return 0;
}
运行结果
完结 明天继续搞