目录
C语言中我们用到的最频繁的输入输出方式就是scanf ()与printf()。
scanf(): 从标准输入设备(键盘)读取数据,并将值存放在变量中。
printf(): 将指定的文字/字符串输出到标准输出设备(屏幕),使用时需要注意宽度输出和精度输出控制。
C++系统实现了一个庞大的类库,其中ios为基类,其他类都是直接或间接派生自ios类

C++标准库提供了4个全局流对象cin、cout、cerr、clog
使用cout进行标准输出,即数据从内存流向控制台(显示器)。
使用cin进行标准输入即数据通过键盘输入到程序中,
同时C++标准库还提供了cerr用来进行标准错误的输出,
以及clog进行日志的输出
从上图可以看出,cout、cerr、clog是ostream类的三个不同的对象,因此这三个对象现在基本没有区别,只是应用场景不同
注意:使用这4个流的时候必须要包含文件并引入std标准命名空间
例如,我们使用cin无法将含空格的字符串"hello world"输入到string对象中
- #include
- using namespace std;
-
- int main() {
-
- //int a = 0;
- //scanf("%d", &a);
- //printf("%d", a);
-
- //cin >> a; // cin是标准库中定义的istream类型全局对象
- //cout << a; // cout是标准库中定义的ostream类型全局对象
- //cin.operator>>(a);
- //cout.operator<<(a);
-
- int a = 1;
- double b = 2.2;
- //C++ cout和cin能自动识别对象类型,因为本质他是一个函数重载区分识别
- cout << a << endl;
- cout << b << endl;
-
- string str;
- // 会调用 std::ios::operator bool()
- while (cin >> str) { // 按ctrl+c 结束
- cout << str << endl;
-
- }
-
- return 0;
- }
对于含有空格的字符串,我们需要使用getline函数进行读取,因为getline函数只有遇到’\n’才会停止读取
- #include
- #include
- using namespace std;
- int main()
- {
- string s;
- getline(cin, s); //输入:"hello world"
- cout << s << endl; //输出:"hello world"
- return 0;
- }
istream类型对象转换为逻辑条件判断值
实际上我们看到使用 while(cin>>i) 去流中提取对象数据时,调用的是operator>>,返回值是istream类型的对象,那么这里可以做逻辑条件值,源自于istream的对象又调用了operator bool,operator bool调用时如果接收流失败,或者有结束标志,则返回false
- // istream& operator>> (int& val);
- // explicit operator bool() const;
- int main(){
- string str;
- // 会调用 std::ios::operator bool()
- while (cin >> str) { // 按ctrl+c 结束
- cout << str << endl;
-
- }
- }
C++根据文件内容的数据格式分为二进制文件和文本文件。采用文件流对象操作文件的一般步骤:
1. 定义一个文件流对象,操作文件的类有以下三个:
使用文件流对象的成员函数打开一个磁盘文件,使得文件流对象和磁盘文件之间建立联系
| 打开方式 | 功能 |
|---|---|
| ios_base::in | 以读的方式打开文件 |
| ios_base::out | 以写的方式打开文件 |
| ios_base::app | 以追加的方式对文件进行写入 |
| ios_base::binary | 以二进制方式对文件进行操作 |
| ios_base::ate | 输出位置从文件的末尾开始 |
| ios_base::trunc | 先将文件内容清空再打开文件 |
以二进制的形式对文件进行写入操作:
- #include
- int main(){
- ofstream ofs("test.txt",ios_base::binary ); //以二进制写入的方式打开
- ofs.put('c'); // fputc()
- ofs.write("11111", 3); // fwrite()
- ofs.close();
- return 0;
- }
以二进制的形式对文件进行读取操作:
- int main(){
- ifstream ifs("test.txt",ios_base::binary ); // 以二进制读取的方式打开
- ifs.seekg(0, ifs.end); //跳转到文件末尾
- int length = ifs.tellg(); //获取当前字符在文件当中的位置,即文件的字符总数
- ifs.seekg(0, ifs.beg); //重新回到文件开头
- char buffer[100];//读取到的数据存在buffer里面
- ifs.read(buffer, length);
- // 处理读取到的数据
- for (int i = 0; i < length; i++)
- {
- cout << buffer[i];
- }
- cout << endl;
- ifs.close();
- return 0;
- }
注意:使用二进制的方式进行读取不能使用string,否则程序会读取异常,虽然在一个进程里面读取没有问题,一个程序写入,另一个程序读取就会出现问题
使用二进制的方式进行操作文件,很不方便,一般都是使用文本的方式操作文件
对文件的操作一般喜欢使用流的方式进行读取,更加方便,可以不使用类的成员的函数
以文本的形式对文件进行写入操作:
- int main(){
- ofstream ofs("test.txt"); // fopen(,"W")
- ofs.put('c'); // fputc()
- ofs.write("11111", 3); // fwrite()
- ofs << "!!!";
-
- ofs.close();
-
- return 0;
- }
以文本的形式对文件进行读取操作:
- int main(){
- ifstream ifs("test.txt"); // fopen(, "R")
- cout << (char)ifs.get() << endl; // fgetc()
- cout << (char)ifs.get() << endl; // fgetc()
- cout << (char)ifs.get() << endl; // fgetc()
- cout << (char)ifs.get() << endl; // fgetc()
- string s;
- ifs >> s; //读取到空格或文件末尾就停下了
- cout << s;
- ifs.close();
- // ifs.read // fread()
-
- return 0;
- }
注意:使用ofstream类对象的open函数时,若不指定打开方式,则默认以写的方式打开文件;使用ifstream类对象的open函数时,若不指定打开方式,则默认以读的方式打开文件;使用fstream类对象的open函数时,若不指定打开方式,则默认以写+读的方式打开文件
- struct info {
- string name;
- int age;
- int score;
- };
-
- int main(){
-
- info win = { "小唐",19 };
- ofstream ofs("test2.txt");
- ofs << win.name << " "; // 这里有分隔符,下面才能这样读
- ofs << win.age << endl; // 分隔符可以是空格或者回车
- ofs.close();
-
- info rin;
- ifstream ifs("test2.txt");
- ifs >> rin.name;
- ifs >> rin.age;
- cout << rin.name << ":" << rin.age << endl;
-
- return 0;
- }
在C语言中,如果想要将一个整形变量的数据转化为字符串格式:
- int main()
- {
- int n = 123456789;
- char s1[32];
- _itoa(n, s1, 10);
-
- char s2[32];
- sprintf(s2, "%d", n);
- char s3[32];
- sprintf(s3, "%f", n);
- return 0;
- }
但是两个函数在转化时,都得需要先给出保存结果的空间,那空间要给多大呢,就不太好界定,而且转化格式不匹配时,可能还会得到错误的结果甚至程序崩溃
在C++中,可以使用stringstream类对象来避开此问题。
在程序中如果想要使用stringstream,必须要包含头文件
stringstream主要可以用来:
1. 将数值类型数据格式化为字符串
- #include
- int main()
- {
- int a = 12345678;
- string sa;
- // 将一个整形变量转化为字符串,存储到string类对象中
- stringstream s;
- s << a;
- s >> sa;
- // clear()
- // 注意多次转换时,必须使用clear将上次转换状态清空掉
- // stringstreams在转换结尾时(即最后一个转换后),会将其内部状态设置为badbit
- // 因此下一次转换是必须调用clear()将状态重置为goodbit才可以转换
- // 但是clear()不会将stringstreams底层字符串清空掉
- // s.str("");
- // 将stringstream底层管理string对象设置成"",
- // 否则多次转换时,会将结果全部累积在底层string对象中
-
- s.str("");
- s.clear(); // 清空s, 不清空会转化失败
- double d = 12.34;
- s << d;
- s >> sa;
- string sValue;
- sValue = s.str(); // str()方法:返回stringsteam中管理的string类型
- cout << sValue << endl;
-
- return 0;
- }
2. 字符串拼接
- int main()
- {
- stringstream sstream;
- // 将多个字符串放入 sstream 中
- sstream << "first" << " " << "string,";
- sstream << " second string";
- cout << "strResult is: " << sstream.str() << endl;
- // 清空 sstream
- sstream.str("");
- sstream << "third string";
- cout << "After clear, strResult is: " << sstream.str() << endl;
- return 0;
- }
序列化和反序列化结构数据
- #include
- struct info {
- string name;
- int age;
- int score;
- };
-
- int main(){
- info win = { "小邓",21 ,99 };
-
- // 序列化成字符串
- ostringstream ost;
- ost << win.name << endl;
- ost << win.age << endl;
- ost << win.score << " ";
-
- string str1 = ost.str();
- cout << str1 << endl;
- // 网络中就可以把这个str1发送给另一端
- //
- //网络另一端接收到以后就可以解析数据
- // 反序列化成字符串
- istringstream ist;
- info rin;
- ist.str(str1);
- ist >> rin.name;
- ist >> rin.age;
- ist >> rin.score;
-
- cout << rin.name << endl;
- cout << rin.age << endl;
- cout << rin.score << " ";
-
- return 0;
- }