键盘输入数据到程序:标准输入input
程序数据输出到显示器:标准输出output
标准输入+标准输出:标准I/O
文件的输出输入:文件I/O
cout:全局流对象 输出数据到显示屏
cerr:标准错误 没有缓冲区
clog:标准日志/错误 有缓冲区
enld除了换行,还会刷新缓冲区
标准输入:#include标准输出:using namespace std; void test01() { //标准输入流cin.get() char c; while ((c = cin.get()) != EOF) //ctrl+z退出 { cout << c << endl; } // 2、 char c2; cin.get(c2); //读取一个字符 char c3[1024] = { 0 }; cin.get(c3, 1024); //从缓冲区读一个字符串 cin.getline(c3, 1024); //从缓冲区读取一行数据 } void test02() { char c; // hello cin.get(c); //h cout << c << endl; //h cin.ignore(); //忽略当前字符 cin.get(c); // l cout << c << endl; // l } void test03() { //此代码存在bug,如12a输出12 cout << "请输入数组或者字符窜" << endl; char c; c = cin.peek(); //偷窥缓冲区返回第一个字符 if (c >= '0' && c <= '9') { int number; cin >> number; cout << "你输入的是数组" << number << endl; } else { char c1[1024] = { 0 }; cin >> c1; cout << "你输入的是字符窜" << c1 << endl; } } void test04() { cout << "请输入数组或者字符窜" << endl; char ch; cin.get(ch); // 从缓冲区取走一个字符 if (ch >= '0' && ch <= '9') { cin.putback(ch); //将ch放回缓冲区 int num; cin >> num; cout << "你输入的是数组" << num << endl; } else { char c1[1024] = { 0 }; cin >> c1; cout << "你输入的是字符窜" << c1 << endl; } } int main() { //test01(); //test02(); //test03(); test04(); }
#include#include #include using namespace std; void test01() { cout << "hello world" << endl; cout.flush();//刷新缓冲区 cout.put('h').put('l') << endl; //输出一个字符 cout.write("hello world",strlen("hello world")); } void test02() { int num = 10; cout.unsetf(ios::dec);//卸载当前默认的10进制输出方式 cout.setf(ios::oct);//换成八进制 cout.setf(ios::showbase); //cout << num << endl; cout.unsetf(ios::oct); cout.setf(ios::hex); //十六进制 //cout << num << endl; cout.widen(10); //设置十个宽度 cout.fill('*'); //空的地方补* cout.setf(ios::left);//设置左对齐 //cout << num << endl; //通过控制符 int num2 = 10; cout << hex << setiosflags(ios::showbase) << setw(10) << setfill('*') << setiosflags(ios::left) << num2 << endl; } int main() { //test01(); test02(); }



