基本概念
输入输出流的数据一般先放在缓冲区中
标准I/O对象:cin、cout、cerr、clog
标准输入流常用函数:get()、ingore()、peek、putback
示例代码
#include <iostream>
using namespace std;
int main()
{
int myInt;
char YourName[50];
cout << "请输入一个Int:";
cin >> myInt;
cout << "你输入的数是:" << endl;
cout << "Int: " << myInt << endl;
cout << "请输入你的姓名:" << endl;
cin >> YourName; //到空格处停止
cout << "你的姓名是:" << endl;
cout << YourName << endl;
char ch;
while((ch = cin.get()) != EOF) //是否读到结束符,get()一次读取一个字符
{
cout << "字符:" << ch << endl;
}
cout << "\n结束\n";
char a,b,c;
cout << "请输入一些字符:" << endl;
cin.get(a).get(b).get(c); //get()读取当前字符,放至参数中,可以连续使用
cout << a << b << c << endl;
char stringOne[256];
char stringTwo[256];
char stringThree[256];
cout << "Enter string one: " ;
cin.getline(stringOne, 256); //使用getline()
cout << "stringOne:" << stringOne << endl;
cout << "Enter string two: ";
cin >> stringTwo; //到一个空格处停止,但是后面的数据仍在缓冲区里
cout << "stringTwo:" << stringTwo << endl;
cout << "Enter string three: ";
cin.getline(stringThree, 256); //当缓冲区是空的时才会getline
cout << "stringThree:" << stringThree << endl; //此时会直接输出stringTwo后面的部分(如果缓冲区仍有数据的话)
//cout相关操作
cout << "Hello World" << endl;
cout.put('H').put('\n');
cout.write("Hello World!\n", 14) << endl; //要给定长度
cout << "Start >";
cout.width(25); //设置宽度
cout.fill('*'); //用*填充
cout << 123 << "< End" << endl;
return 0;
}