目录
(1)在C语言当中,我们使用最频繁的输入输出方式就是scanf与printf:
C语言借助了相应的缓冲区来进行输入与输出:

(2)对输入输出缓冲区的理解:
C++系统实现了一个庞大的类库,其中ios为基类,其他类都是直接或间接派生自ios类 :

(1)C++标准IO流
C++标准库提供了4个全局流对象(cin、cout、cerr、clog):
从上图可以看出,cout、cerr、clog都是由ostream类实例化出的三个不同的对象,因此这三个对象基本没什么区别,只是应用场景不同
①在使用cin、cout时必须要包含iostream文件,并引入std标准命名空间
- #include
//包含iostream文件 - using namespace std; //引入std标准命名空间
-
- int main()
- {
- int a = 0;
- cin >> a;
- cout << a << endl;
- return 0;
- }
或是在使用时指定cout和cin所属的命名空间
- #include
//包含iostream文件 - int main()
- {
- int a = 0;
- std::cin >> a; //使用时指定所属命名空间
- std::cout << a << std::endl; //使用时指定所属命名空间
- return 0;
- }
② cin为缓冲流。键盘输入的数据保存在缓冲区中,当要提取时,是从缓冲区中提取。如果一次输入过多,则多余的数据会留在缓冲区以供之后提取,如果输入错了,必须在回车之前进行修改,回车键按下就无法进行修改了,只有把输入缓冲区中的数据取完后,才会要求输入新的数据。
- #include
- using namespace std;
-
- int main()
- {
- int a = 0, b = 0;
- cin >> a; //输入:1 2
- cout << a << endl;
-
- cin >> b; //直接从输入缓冲区提取
- cout << b << endl;
- return 0;
- }
③输入数据类型必须与要提取的数据类型一致,否则出错。出错只是在流的状态字state中对应位置(置1),程序继续。
④空格和回车都可以作为数据之间的分隔符,所以多个数据可以在一行输入,也可以分行输入。但如果是字符型和字符串,则空格无法用cin输入,字符串中也不能有空格,回车符也无法读入。
- #include
- #include
- using namespace std;
-
- int main()
- {
- string s;
- cin >> s; //输入:"hello world"
- cout << s << endl; //输出:"hello"
- return 0;
- }
- #include
- #include
- using namespace std;
-
- int main()
- {
- string s;
- getline(cin, s); //输入:"hello world"
- cout << s << endl; //输出:"hello world"
- return 0;
- }
⑤. cin和cout可以直接输入和输出内置类型的数据。因为标准库已经将所有内置类型的输入和输出进行了重载。


⑥对于自定义类型,如果要支持cin和cout的标准输入输出,则需要对<<和>>进行重载。
- #include
- using namespace std;
- class Date
- {
- -设置为友员,类外函数可以访问类内变量
- friend istream& operator>>(istream& in, Date& d);
- friend ostream& operator<<(ostream& out, const Date& d);
- public:
- Date(int year = 0, int month = 0, int day = 0)
- :_year(year)
- , _month(month)
- , _day(day)
- {}
- private:
- int _year;
- int _month;
- int _day;
- };
-
- //对>>进行重载
- istream& operator>>(istream& in, Date& d)
- {
- in >> d._year >> d._month >> d._day;
- return in;
- }
-
- //对<<进行重载
- ostream& operator<<(ostream& out, const Date& d)
- {
- out << d._year << "-" << d._month << "-" << d._day;
- return out;
- }
-
- int main()
- {
- Date d;
- cin >> d; //输入:1 2 3
- cout << d << endl; //输出:1-2-3
- return 0;
- }
⑦.在线OJ中的输入和输出。
- while (scanf("%d", &a) != EOF)
- {
- //...
- }
- while (cin >> a)
- {
- //...
- }
⑧建议
①定义一个文件流对象 , 操作文件的类有三个
| 类 | 对应操作场景 |
|---|---|
| ofstream | 将数据写入文件 |
| ifstream | 将数据读出文件 |
| fstream | 输入输出都能用(读+写) |
②使用文件流对象的成员函数打开一个磁盘文件,使得文件流对象和磁盘文件之间建立联系。
| 打开方式 | 功能 |
|---|---|
| in | 以读的方式打开文件 |
| out | 以写的方式打开文件 |
| binary | 以二进制方式对文件进行操作 |
| ate | 输出位置从文件的末尾开始 |
| app | 以追加的方式对文件进行写入 |
| trunc | 先将文件内容清空再打开文件 |
③使用提取和插入运算符对文件进行读写操作,或使用成员函数进行读写。
| 成员函数 | 功能 |
|---|---|
| put | 插入一个字符到文件 |
| write | 插入一段字符到文件 |
| get | 从文件提取字符 |
| read | 从文件提取多个字符 |
| tellg | 获取当前字符在文件当中的位置 |
| seekg | 设置对文件进行操作的位置 |
| >>运算符重载 | 将数据形象地以“流”的形式进行输入 |
| <<运算符重载 | 将数据形象地以“流”的形式进行输出 |
④关闭文件。
文件流对象.close();
(1)读取字符的三种方式
- void readfile()
- {
- ifstream ifs("test.cpp");
- char ch;
-
- -1
- while ((ch = ifs.get()) != eof)
- {
- cout << ch;
- }
-
- -2
- while (ifs.get(ch))
- {
- cout << ch;
- }
-
- -3
- while (ifs)
- {
- cout << (char)ifs.get();
- }
- }
(2)以二进制的方式操作文件
- //以二进制的形式对文件进行写入
- void WriteBinary()
- {
- ofstream ofile; //定义文件流对象- 只写
- ofile.open("test.bin", ofstream::out | ofstream::binary); //以二进制写入的方式打开test.bin文件
- char data[] = "2022-8-16";
- ofile.write(data, strlen(data)); //将data字符串写入文件
- ofile.put('!'); //将字符'!'写入文件
- ofile.close(); //关闭文件
- }
- //以二进制的形式对文件进行读取
- void ReadBinary()
- {
- ifstream ifile; //定义文件流对象-读
- ifile.open("test.bin", ofstream::in | ofstream::binary); //以二进制读取的方式打开test.bin文件
- ifile.seekg(0, ifile.end); //跳转到文件末尾
- int length = ifile.tellg(); //获取当前字符在文件当中的位置,即文件的字符总数
- ifile.seekg(0, ifile.beg); //重新回到文件开头
- char data[100];
- ifile.read(data, length); //将文件当中的数据全部读取到字符串data当中
-
- cout << data << endl;
- ifile.close(); //关闭文件
- }
(3)以文本方式操作文件
- //以文本的形式对文件进行写入
- void WriteTxt()
- {
- ofstream ofile; //定义文件流对象
- ofile.open("test.txt"); //以写入的方式打开test.txt文件
- char data[] = "2022-8-16";
- ofile.write(data, strlen(data)); //将data字符串写入文件
- ofile.put('!'); //将字符'!'写入文件
- ofile.close(); //关闭文件
- }
- //以文本的形式对文件进行读取
- void ReadTxt()
- {
- ifstream ifile; //定义文件流对象
- ifile.open("test.txt"); //以读取的方式打开test.txt文件
- ifile.seekg(0, ifile.end); //跳转到文件末尾
- int length = ifile.tellg(); //获取当前字符在文件当中的位置,即文件的字符总数
- ifile.seekg(0, ifile.beg); //重新回到文件开头
- char data[100];
- ifile.read(data, length); //将文件当中的数据全部读取到字符串data当中
-
- cout << length << endl;
- cout << data << endl;
- ifile.close(); //关闭文件
- }
(4)使用>>和<<对文件进行操作
- //对文件进行写入操作
- void WriteFile()
- {
- ofstream ofs("data.txt"); //定义文件流对象,并打开文件
- ofs << "2022-8-16"; //字符串“流入”文件
- ofs.close(); //关闭文件
- }
- //对文件进行读取操作
- void ReadFile()
- {
- ifstream ifs("data.txt"); //定义文件流对象,并打开文件
- char data[100];
- ifs >> data; //文件数据“流入”字符串data
- ifs.close(); //关闭文件
- }
(5)补充
(1)在C语言中,我们若是想要将一个整型变量的数据转化为字符串格式有两种方法
- int a = 10;
- char arr[10];
- itoa(a, arr, 10); //将整型的a转化为十进制字符数字存储在字符串arr当中
- int a = 10;
- char arr[10];
- sprintf(arr, "%d", a); //将整型的a转化为字符串格式存储在字符串arr当中
(2)stringstream类对象
| 类 | 对应操作场景 |
|---|---|
| ostringstream | 输出操作 |
| istringstream | 输入操作 |
| stringstream | 输入操作+输出操作 |
①使用stringstream类,将数值类型数据格式化为字符串
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- int a = 10;
- string sa;
- stringstream s;
- s << a; //将int类型的a放入输入流
- s >> sa; //从s中抽取前面插入的int类型的值,赋值给string类型(方式一)
- cout << sa << endl;
-
- s.str(""); //将stringstream底层管理的string对象设置为""。
- s.clear(); //将上次转换状态清空掉
-
- //进行下一次转换
- double b = 3.14;
- s << b;
- sa = s.str(); //获取stringstream中管理的string类型(方式二)
- cout << sa << endl; // 3.14
- return 0;
- }
// clear()// 注意多次转换时,必须使用 clear 将上次转换状态清空掉// stringstreams 在转换结尾时 ( 即最后一个转换后 ), 会将其内部状态设置为 badbit// 因此下一次转换是必须调用 clear() 将状态重置为 goodbit 才可以转换// 但是 clear() 不会将 stringstreams 底层字符串清空掉// s.str("");// 将 stringstream 底层管理 string 对象设置成 "",// 否则多次转换时,会将结果全部累积在底层 string 对象中
②使用stringstream类,字符串拼接
- #include
- #include
- #include
- using namespace std;
- int main()
- {
- string rets;
- int ret1;
- char ret2[10];
- stringstream s;
- s << "2022" << "8-16"; //将多个字符串放入stringstream中
- s >> rets; //方式一获取
- cout << rets << endl;
-
-
- -通过空格分割
- /*
- s.str(""); //将stringstream底层管理的string对象设置为空字符串
- s.clear(); //将上次转换状态清空掉
- s << "2022" << " " << "8-16";
- s >> ret1 >> ret2;
- cout << ret1 <
- cout << ret2 << endl; - 8-16
- */
-
- s.str(""); //将stringstream底层管理的string对象设置为空字符串
- s.clear(); //将上次转换状态清空掉
-
- s << "Xiao" << " " << "Mi" << " " << "MIUI"; //将多个字符串放入stringstream中
- rets = s.str(); //方式二获取
- cout << rets << endl;
- return 0;
- }
(3)注意事项