目录
1、C语言的输入输出
2、流是什么
3、C++IO流
3.1、C++标准IO流
3.2、C++文件IO流
文件操作步骤
以二进制的形式操作文件
以文本的形式操作文件
4、stringstream的介绍
C语言中我们用到的最频繁的输入输出方式就是scanf()和printf()。
- scanf():从标准输入设备(键盘)读取数据,并将值存放在变量中
- printf():将指定的文字/字符串输出到标准输出设备(屏幕),使用时注意宽度输出和精度输出控制。
C语言借助了相应的缓冲区来进行输入与输出,如下图所示:
对输入输出缓冲区的理解:
- 可以屏蔽掉低级I/O的实现,低级I/O的实现依赖操作系统本身内核的实现,所以如果能够屏蔽这部分的差异,可以很容易写出可移植的程序。
- 可以使用这部分的内容实现“行”读取的行为,对于计算机而言是没有“行”这个概念,有了这部分,就可以定义“行”的概念,然后解析缓冲区的内容,返回一个“行”。
scanf与printf是和控制台、终端相关的,还有与文件相关的(fscanf、fprintf)
- fscanf:针对所有输入流的格式化的输入语句 —— stdin/文件
- fprintf:针对所有输出流的格式化输出语句 —— stdout/文件
还有用来序列化字符串的(sscanf、sprintf)
- sscanf:从一个字符串中读取一个格式化的数据
- sprintf:把一个格式化的数据,转换成字符串
上述是C语言管理字符串序列号、反序列化的方式,C++的流也是按照类似的方式实现的。
- “流”即是流动的意思,是物质从一处向另一处流动的过程,是对一种有序连续且具有方向性的数据( 其单位可以是bit,byte,packet )的抽象描述。
- C++流是指信息从外部输入设备(如键盘)向计算机内部(如内存)输入和从内存向外部输出设备(显示器)输出的过程。这种输入输出的过程被形象的比喻为“流”。
它的特性是:有序连续、具有方向性。为了实现这种流动,C++定义了I/O标准类库,这些每个类都称为流/流类,用以完成某方面的功能
C++系统实现了一个庞大的类库,其中ios为基类,其他类都是直接或间接派生自ios类。
C++标准库提供了4个全局流对象cin、cout、cerr、clog:
- 使用cout进行标准输出,即数据从内存流向控制台(显示器)
- 使用cin进行标准输入即数据通过键盘输入到程序中
- C++标准库还提供了cerr用来进行标准错误的输出
- 以及clog进行日志的输出
从上图可以看出,cout、cerr、clog是ostream类的三个不同的对象,因此这三个对象现在基本没有区别,只是应用场景不同。
注意:
1、在使用时候必须要包含文件并引入std标准命名空间,或是在使用时指定cout和cin所属的命名空间。
#include
//包含iostream文件 using namespace std;//引入std标准命名空间 int main() {int a;cin >> a;cout << a << endl;std::cin >> a;//使用时指定所属命名空间std::cout << a << std::endl;//使用时指定所属命名空间return 0; } 2、cin为缓冲流。键盘输入的数据保存在缓冲区中,当要提取时,是从缓冲区中提取,如果一次输入过多,则多余的数据会留在缓冲区以供之后提取,如果输入错了,必须在回车之前进行修改,回车键按下就无法进行修改了,只有把输入缓冲区的数据取完后,才会要求输入新的数据。
- 例如:如下的代码,若在第一次输入时输入两个数字,中间以空格间隔开,空格后的数据就是多余的数据,会存留在缓冲区,等到下一次输入时,会直接从缓冲区提取前面多余的数据。
#include
using namespace std; int main() {int a, b, c;cin >> a;//输入10 20 30cout << a << endl;//10cin >> b;//直接从缓冲区提取数据cout << b << endl;//20cin >> c;//直接从缓冲区提取数据cout << c << endl;//30return 0; } 3、输入的数据类型必须与要提取的数据类型一致,否则出错,出错只是在流的状态字state中对应位置(置1),程序继续。
4、空格和回车都可以作为数据之间的分格符,所以多个数据可以在一行输入,也可以分行输入。但如果是字符型和字符串,则空格(ASCII码为32)无法用cin输入,字符串中也不能有空格。回车符也无法读入。
- 例如:我们使用cin无法把含有空格字符的字符串"hello world"输入到string对象中。
#include
#include using namespace std; int main() {string str;cin >> str;//输入"hello world"cout << str << endl;//输出"hello"return 0; }
- 对于含有空格的字符串,我们可以使用getline函数来解决此问题,因为getline函数只有遇到'\n'换行符才会停止。
#include
#include using namespace std; int main() {string str;getline(cin, str);//输入"hello world"cout << str << endl;//输出"hello world"return 0; } 5、cin和cout可以直接输入和输出内置类型数据,因为标准库已经将所有内置类型的输入和输出全部重载了。
- >>运算符重载:
- <<运算符重载:
6、对于自定义类型,如果要支持cin和cout的标准输入输出,则需要对<<和>>进行重载。
- 如下简易实现的自定义类型日期类,支持了对cin和cout的重载,就可以直接进行cin输入和cout输出了
#include
using namespace std; class Date {//友元函数friend ostream& operator<<(ostream& out, const Date& d);//流插入 < >(istream& in, Date& d);//流提取 >> public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;} private:int _year;int _month;int _day; }; //流插入 << ostream& operator<<(ostream& out, const Date& d) {out << d._year << "-" << d._month << "-" << d._day << endl;return out; } //流提取 >> istream& operator>>(istream& in, Date& d) {in >> d._year >> d._month >> d._day;return in; } int main() {Date d;cin >> d;//2022 11 20cout << d << endl;//2022-11-20 } 7、在线OJ中的输入和输出 && 8、istream类型对象转换为逻辑条件判断值
- 对于IO类型的算法,一般都需要循环输入,对于C语言,我们是这样写的:
int main() {char buff[128];while (scanf("%s", buff) != EOF){//……}return 0; }
一旦程序运行,就会陷入循环,如果我们想要结束此循环,在C语言中我们通常有两种方法:
- ctrl + z 加 换行
- ctrl + c(强制杀掉进程)
在C++中,要想循环输入,我们是这样解决的:
int main() {string str;while (cin >> str){cout << str << endl;}return 0; }
对于C++,我想要终止此循环,该如何解决的呢?
首先,我cin>>str实际上调用的是string类中的operator>>(cin, str)运算符重载,相当于是拿此运算符重载的返回值来解决何时终止循环的问题。来看下库里的整体框架:
由此可见,此函数的返回值就是cin,也就是istream类型的对象,可istream类型的对象(cin)本身不能作为这里循环的条件逻辑判断,但是C++支持一种语法,可以支持自定义类型的隐式类型转换,在讲解此问题之前,先来看看如下的样例:
- C++有一个运算符重载比较特殊,可以支持把自定义类型转换为内置类型,看如下的自定义类型日期类:
class Date {friend ostream& operator << (ostream& out, const Date& d);friend istream& operator >> (istream& in, Date& d); public:Date(int year = 1, int month = 1, int day = 1):_year(year), _month(month), _day(day){}//支持Date对象转换为bool类型operator bool(){//这里是随意写的,假设输入_year为0,则结束if (_year == 0)return false;elsereturn true;}//支持Date对象转换为int类型operator int(){return _year + _month + _day;} private:int _year;int _month;int _day; }; int main() {//内置类型转自定义类型(隐式类型转换)Date d1 = 0;Date d2 = { 2022, 11, 20 };//自定义类型转换为内置类型(bool)bool ret1 = d1;bool ret2 = d2;cout << ret1 << endl;//0cout << ret2 << endl;//1if (d2){cout << "operator ()" << endl;//发生隐式类型转换}//自定义类型转换为内置类型(int)int i = d2;cout << i << endl;//2053return 0; }
C++对实现类似于下面的函数,就可以支持自定义类型转为内置类型
operator bool(){} operator int(){}
- 转换过程如下:先调用operator bool()与operator int()的重载函数,调用函数之后返回值产生的一个临时变量,然后再赋值给另一变量,而这个临时变量我们是可以自己定义的,以及函数的逻辑也是我们定义的。
- 上述main函数内部的if条件中,d2会进行隐式类型转换,转换成调用operator bool()函数,内部返回的是true。
所以 为什么 (cin>>str 可以做判断的条件呢?)
- cin>>str实际上调用的是string类中的operator>>(cin, str)运算符重载,此函数返回的是istream类型的对象(cin),既然它能做条件判断,那么istream内部必定是调用了operator bool()运算符重载函数,来看看库里的截图:
- 由此可见,实际上operator bool()运算符重载是ios基类实现的,不过istream类继承了ios类,自然能够调用父类ios的成员函数,所以这里cin >> str(cin. operator bool())的返回值就可以作为循环结束的条件判断了。
总结:
- 实际上我们看到使用while(cin>>i)去流中提取对象数据时,调用的是operator>>,返回值是istream类型的对象,那么这里可以做逻辑条件值,源自于istream的对象又调用了operator bool,operator bool调用时如果接收流失败,或者有结束标志,则返回false。
下面展开讨论。
C++根据文件内容的数据格式分为二进制文件和文本文件。采用文件流对象操作文件的一般步骤如下:
1、定义一个文件流对象
类 对应操作场景 ifstream 只读、只输入用 ofstream 只写、只输出用 fstream 读 + 写、既输入又输出用 2、使用文件流对象的成员函数打开一个磁盘文件,使得文件流对象和磁盘文件之间建立联系。文件常见的打开方式如下:
打开方式 功能 in 以读的方式打开文件 out 以写的方式打开文件 binary 以二进制的方式对文件进行操作 ate 输出位置从文件的末尾开始 app 以追加的方式对文件进行写入 trunc 先将文件内容清空再打开文件 3、使用提取和插入运算符对文件进行读写操作,或使用成员函数进行读写,其中对文件进行提取和插入操作的常用成员函数如下:
成员函数 功能 put 插入一个字符到文件 write 插入一段字符到文件 get 从文件提取字符 read 从文件提取多个字符 tellg 获取当前字符在文件当中的位置 seekg 设置对文件进行操作的位置 >>运算符重载 将数据形象地以“流”的形式进行输入 <<运算符重载 将数据形象地以“流”的形式进行输出 4、关闭文件
- 示例:我读一个文件(test.cpp)
#include
#include using namespace std; int main() {ifstream ifs("test.cpp");//法一:while (ifs){char ch = ifs.get();cout << ch;}/*法二char ch;while (ifs >> ch){cout << ch;这里读出的结果不换行,过滤掉了空格和换行}*/return 0; }
再比如我要拷贝一份test.cpp文件生成copy.cpp
int main() {ifstream ifs("test.cpp");ofstream ofs("copy.cpp");char ch = ifs.get();while (~ch){ofs << ch;ch = ifs.get();}return 0; }
以二进制形式读对文件进行写入操作:
//以二进制的形式对文件进行写入 int main() {ofstream ofile; //定义文件流对象ofile.open("test.txt", ofstream::out | ofstream::binary); //以二进制写入的方式打开test.txt文件char array[] = "hello world";ofile.write(array, strlen(array)); //将array字符串写入文件ofile.put('#'); //将字符'#'写入文件ofile.close(); //关闭文件//此时test.txt文件仅有"hello world#"return 0; }
以二进制的形式对文件进行读取操作:
//以二进制的形式对文件进行读取 int main() {ifstream ifile;//定义文件流对象ifile.open("test.txt", ofstream::in | ofstream::binary);//以二进制的读取的方式打开test.doc文件ifile.seekg(0, ifile.end);//跳转到文件末尾int len = ifile.tellg();//获取当前字符在文件当中的位置,既文件的字符总数ifile.seekg(0, ifile.beg);;//重新回到文件开头char array[100];ifile.read(array, len);//将文件当中的数据全部读取到字符串array当中cout << len << endl;cout << array << endl;ifile.close();//关闭文件return 0; }
再比如我现在有一个ServerInfo的类
struct ServerInfo {char _address[32];int _port; };
现在想要把它写到文件,我们有两种方式:
- 二进制的方式
- 文本的方式
我们先以二进制的方式写入:
struct ServerInfo {const char _address[256] = "";int _port; }; struct ConfigManager { public:ConfigManager(const char* filename):_filename(filename){}//以二进制写void WriteBin(const ServerInfo& info){ofstream ofs(_filename, ios_base::out | ios_base::binary);ofs.write((const char*)&info, sizeof(info));}//以二进制读void ReadBin(ServerInfo& info){ifstream ifs(_filename, ios_base::in | ios_base::binary);ifs.read((char*)&info, sizeof(info));} private:string _filename; // 配置文件 }; int main() {//以二进制写ServerInfo winfo = { "https://legacy.cplusplus.com/reference/ios/", 80 };ConfigManager cf_bin("test.bin");cf_bin.WriteBin(winfo);//以二进制读ServerInfo rbinfo;cf_bin.ReadBin(rbinfo);cout << rbinfo._address << " " << rbinfo._port << " ";//https://legacy.cplusplus.com/reference/ios/ 80return 0; }
以文本的形式对文件进行写入操作:
//以文本的形式对文件进行写入 int main() {ofstream ofile;//定义文件流对象ofile.open("test.txt");//以写入的方式打开test.txt文件char array[] = "hello world";ofile.write(array, strlen(array));//将array字符串写入文件ofile.put('#');//将字符'#'写入文件ofile.close();//关闭文件return 0; }
以文本的形式对文件进行读取操作:
//以文本的形式对文件进行读取 int main() {ifstream ifile;//定义文件流对象ifile.open("test.txt");//以读取的方式打开test.txt文件ifile.seekg(0, ifile.end);//跳转到文件末尾int len = ifile.tellg();//获取当前字符在文件中的位置,即文件的字符总数ifile.seekg(0, ifile.beg);//重新回到文件开头char array[100];ifile.read(array, len);//将文件中的数据全部读取到字符串data中cout << len << endl;cout << array << endl;ifile.close();//关闭文件return 0; }
再比如下面的类,并将其改成以文本的方式读取和写入:
class Date {//友元函数friend ostream& operator<<(ostream& out, const Date& d);//流插入 <
>(istream& in, Date& d);//流提取 >> public:Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;} private:int _year;int _month;int _day; }; //流插入 << ostream& operator<<(ostream& out, const Date& d) {out << d._year << "-" << d._month << "-" << d._day << endl;return out; } //流提取 >> istream& operator>>(istream& in, Date& d) {in >> d._year >> d._month >> d._day;return in; } struct ServerInfo {string _address;int _port;Date _d; }; struct ConfigManager { public:ConfigManager(const char* filename):_filename(filename){}//以文本方式写void WriteText(const ServerInfo& info){ofstream ofs(_filename);ofs << info._address << endl;ofs << info._port << endl;ofs << info._d << endl;}//以文本方式读void ReadText(ServerInfo& info){ifstream ifs(_filename);ifs >> info._address;ifs >> info._port;ifs >> info._d;} private:string _filename; // 配置文件 }; int main() {ServerInfo winfo = { "https://legacy.cplusplus.com/reference/ios/", 80, { 2022, 11, 22 } };//以文本方式写ConfigManager cf_txt("test.txt");cf_txt.WriteText(winfo);//以文本方式读ConfigManager cf_text("test.txt");ServerInfo rtinfo;cf_text.ReadText(rtinfo);cout << rtinfo._address << " " << rtinfo._port << " " << rtinfo._d << endl;//https://legacy.cplusplus.com/reference/ios/ 80 2022--11--22return 0; } 注意:
- C++文件流的优势就是可以对内置类型和自定义类型,都使用一样的方式,去流插入和流提取数据,当然这里自定义类型Date需要重载>> 和 <<
在C语言中,如果想要将一个整形变量的数据转化为字符串格式,有如下两种方法:
- 1、使用itoa()函数
int main() {int n = 123456789;char s1[32];_itoa(n, s1, 10);//将整型的a转化为十进制字符数字存储在字符串s1中//_itoa(n, s1, 2);以二进制形式存在字符串s1中return 0; }
- 2、使用sprintf函数
int main() {int n = 123456789;char s2[32];sprintf(s2, "%d", n);//将整型的n转化为字符串格式存储在字符串s2当中return 0; }
- 虽然上述俩函数都能够完成整型到字符串的转化,但是两个函数在转化时,都得需要先给出保存结果的空间,那空间要给多大呢,就不太好界定,而且转化格式不匹配时,可能还会得到错误的结果甚至程序崩溃。
在C++中,我们可以使用stringstream类对象来避开此问题。在程序中如果想要使用stringstream,必须要包含头文件
。在该头文件下,标准库有如下三个类:
类 对应操作场景 ostringstream 输出操作 istringstream 输入操作 stringstream 输入操作 + 输出操作 来看这样一幅图:
- 上图足矣说明,istringstream继承了istream,ostringstream继承了ostream,而iostream继承了istream和ostream,库里相当于是完成了一个菱形继承。stringstream是istringstream和ostringstream两个功能的合体。本文主要介绍stringstream,其作用如下:
一、将数值类型数据格式化为字符串
- ostringstream把数据类型转化为字符串
- istringstream把字符串转化为数据类型
#include
#include using namespace std; int main() {//ostringstream把数据类型转为字符串int i = 123;ostringstream oss;oss << i;//把i流插入到ossstring stri = oss.str();oss.str("");//将ostringstream底层管理的string对象设置为""。s.clear(); //将上次转换状态清空掉double d = 44.55;oss << d;//把d流插入到ossstring strd = oss.str();//istringstream把字符串转为数据类型istringstream iss(strd);double n;iss >> n;//n = 44.55return 0; }
- stringstream综合istringstream和ostringstream的功能
#include
#include #include using namespace std; int main() {int n = 10;string str;stringstream ss;ss << n; //将int类型的n放入输入流ss >> str; //从ss中抽取前面插入的int类型的值,赋值给string类型(方式一)cout << str << endl;//10ss.str(""); //将stringstream底层管理的string对象设置为""。ss.clear(); //将上次转换状态清空掉//进行下一次转换double b = 44.55;ss << b;str = ss.str(); //获取stringstream中管理的string类型(方式二)cout << str << endl;//44.55return 0; } 注意:
- clear():注意多次转换时,必须使用clear将上次转换状态清空掉,stringstreams在转换结尾时(即最后一个转换后),会将其内部状态设置为badbit,因此下一次转换是必须调用clear()将状态重置为goodbit才可以转换,但是clear()不会将stringstreams底层字符串清空掉。
- s.str(""):将stringstream底层管理string对象设置成"",否则多次转换时,会将结果全部累积在底层string对象中
二、字符串拼接
#include
#include using namespace std; int main() {string str;stringstream sstream;// 将多个字符串放入 sstream 中sstream << "hello" << " " << "world";sstream << " !!!";sstream >> str;cout << str << endl;//hellocout << "strResult is: " << sstream.str() << endl;//strResult is: hello world !!!// 清空 sstreamsstream.str("");sstream.clear();sstream << "third string";str = sstream.str();cout << str << endl;//third stringcout << "After clear, strResult is: " << sstream.str() << endl;//After clear, strResult is: third stringreturn 0; } 三、序列化和反序列化结构数据
- 序列化:把一些值(整型、浮点型、自定义类型……)的数据转为字符串
- 反序列化:把字符串转化为整数
这个和第一点差不多,示例如下:我们日常聊天中,假如要把如下的信息发送过去:
struct ChatInfo {string _name; // 名字int _id; // idDate _date; // 时间string _msg; // 聊天信息 };
这里的信息是个结构体对象,若要把此结构体信息发送给你,就需要运用到序列化和反序列化:
int main() {// 结构信息序列化为字符串ChatInfo winfo = { "张三", 135246, { 2022, 11, 24 }, "晚上一起看电影吧" };ostringstream oss;oss << winfo._name << " " << winfo._id << " " << winfo._date << " " << winfo._msg;//把结构体信息插入到oss流string str = oss.str();//转换成字符串cout << str << endl << endl;// 我们通过网络这个字符串发送给对象,实际信息相对更复杂,这里做简化// 字符串解析成结构信息ChatInfo rInfo;istringstream iss(str);iss >> rInfo._name >> rInfo._id >> rInfo._date >> rInfo._msg;cout << "-------------------------------------------------------" << endl;cout << "姓名:" << rInfo._name << "(" << rInfo._id << ") ";cout << rInfo._date << endl;cout << rInfo._name << ":>" << rInfo._msg << endl;cout << "-------------------------------------------------------" << endl;return 0; }
总结:
- stringstream实际是在其底层维护了一个string类型的对象用来保存结果。
- 多次数据类型转化时,一定要用clear()来清空,才能正确转化,但clear()不会将stringstream底层的string对象清空。
- 可以使用s. str("")方法将底层string对象设置为""空字符串。
- 可以使用s.str()将让stringstream返回其底层的string对象。
- stringstream使用string类对象代替字符数组,可以避免缓冲区溢出的危险,而且其会对参数类型进行推演,不需要格式化控制,也不会出现格式化失败的风险,因此使用更方便,更安全。