接口网站建设,企业管理软件定制开发,网络营销是什么300字,python 菜鸟教程文章目录 1、C标准IO流2、C文件IO流1、二进制读写2、文本读写 3、stringstream 1、C标准IO流
C语言的printf和scanf无法很好的输入输出自定义类型#xff0c;且还需要程序员自己确定类型#xff0c;所以C就引入了输入流和输出流#xff0c;是设备和内存之间的沟通。 其实io… 文章目录 1、C标准IO流2、C文件IO流1、二进制读写2、文本读写 3、stringstream 1、C标准IO流
C语言的printf和scanf无法很好的输入输出自定义类型且还需要程序员自己确定类型所以C就引入了输入流和输出流是设备和内存之间的沟通。 其实iostream是通过菱形继承实现的。coutcerrclog其实做得有时候没有区分
int main()
{cout 1111 endl;cerr 2222 endl;return 0;
}也都能打印出来。fstream和sstream是针对文件和字符串的对应的就是fprintf/fscanffwrite/freadsprintf/sscanf。CIO流的特点就是面向对象以及能更好地支持自定义类对象的IO。
istream和ostream是防止拷贝的。
CIO流的缓冲区和C语言的缓冲区会进行同步比如用cout和printf混合着输出也能输出。默认开着同步也可以关闭同步增强效率。和这个接口有关 实现了迭代器的容器通常就不需要重载IO流。 string str;while (cin str){cout str endl;}这样的写法就可以持续写入字符串想要停止就用ctrl z 换行来停止这样是将流对象提取到结束如果是ctrl c就是信号强杀进程。我们要知道此时cin str是在做什么右边的数据会被获取给到cin如果有多个那就获取多个最终都流向cin然后返回一个istream类型的对象这个对象会被转为bool类型来进行判断但是一些自定义类型不可以转像string而istream可以转是因为类里重载了一个bool()函数支持自定义类型转内置类型。
在之前的博客中写到过单参数构造的类支持隐式类型转换
class A
{
public:A(int a):_a(a){}
private:int _a;
};int main()
{A aa1 1;return 0;
}这里的内部实现其实是构造一个临时对象然后用这个对象来拷贝构造aa1如果是A就不行了。这是内置类型转自定义类型但是自定义转内置不可int x aa1。解决办法就是重载一个类型()函数。 operator int(){return _a;}如果该函数前面加上explicit就不行了不过在下面写int x (int)aa1强制转换一下也行。
istream里的operator bool()如果被设置成错误的信息就返回false否则返回true错误的信息例如ctrl Z 换行。
2、C文件IO流
1、二进制读写
#include fstream
using namespace std; int main()
{ofstream ofs(F://test.txt);ofs hello world;return 0;
}默认是覆盖写第二个模板参数默认是ios_base::openmode mode ios_base::outios_base是ofstream和ifstream的父类如果想要用其它写入模式 是用 | 来隔开的。
int main()
{ofstream ofs(F://test.txt, ofstream::out | ofstream::app);ofs hello world;return 0;
}看一个读写都有的代码
struct ServerInfo
{char _address[32];int _port;Date _date;
};struct ConfigManager
{
public:ConfigManager(const char* filename):_filename(filename){}void WriteBin(const ServerInfo info){ofstream ofs(_filename, ofstream::out | ofstream::binary);ofs.write((const char*)info, sizeof(info));}void ReadBin(ServerInfo info){ifstream ifs(_filename, ofstream::in | ofstream:::binary);ifs.read((char*)info, sizeof(info));}private:string _filename; // 配置文件
};int main()
{ServerInfo winfo { 192.0.0.1, 80, {2023, 9, 4} };string str;cin str;if (str 二进制写){ConfigManager cm(F://test.txt);cm.WriteBin(winfo);}else if (str 二进制读){ServerInfo rinfo;ConfigManager cm(F://test.txt);cm.ReadBin(rinfo);cout rinfo._address endl;cout rinfo._port endl;cout rinfo._date endl;}return 0;
}二进制读写对象中不能用string能读进去但是会崩而且如果输入的字符串比较长那基本上就是崩掉。
二进制写时写到文件的是string对象及里面指向空间的指针程序结束string自动析构指针指向的空间销毁了所以就没法读取读到的就是野指针。
二进制读写的优点就是简单易操作缺点就是没法看到实体。
2、文本读写
C语言文本读写的本质是内存中任何类型都转成字符串来交互C封装运算符重载后就变得更高效了。 void WriteText(const ServerInfo info){ofstream ofs(_filename);ofs info._address info._port info._date;}日期类中实现了对流插入流提取的重载不仅ostream能使用ofstream和osstream都可以用因为使用了继承体系ostream是它们的父类。istream也是如此。 void WriteText(const ServerInfo info){ofstream ofs(_filename);ofs info._address endl;ofs info._port endl;ofs info._date endl;}void ReadText(ServerInfo info){ifstream ifs(_filename);ifs info._address;ifs info._port;ifs info._date;}else if (str 文本写){ConfigManager cm(F://test.txt);cm.WriteText(winfo);}else if (str 文本读){ServerInfo rinfo;ConfigManager cm(F://test.txt);cm.ReadText(rinfo);cout rinfo._address endl;cout rinfo._port endl;cout rinfo._date endl;}3、stringstream
对应C语言的sprintf和sscanf。 就像最上面那个图中显示ios_base是一个基类ios是它的派生类istream和ostream是ios的派生类iostream是istream和ostream的组合然后istreamostreamiostream各有分支stingstream继承于iostreamistringstream继承于istreamostringstream继承于ostream。
头文件是sstream不是stringstream。
int main()
{ostringstream oss;oss 100 ;oss 11.22 ;oss hello wolrd endl;string str oss.str();cout str endl;return 0;
}可以把流插入的内容转换成一个字符串也可以提取出来。
int main()
{ostringstream oss;oss 100 ;oss 11.22 ;oss hello wolrd endl;string str oss.str();cout str endl;istringstream iss(str);int i;double d;string s;iss i d s;cout i endl;cout d endl;cout s endl;return 0;
}iss后面的顺序也得和oss拿到的顺序一样要不然会使得某些变量内容不对但也能打印出来。oss和iss也可以换成stringstream的类型。
这个功能主要用于序列化和反序列化
struct ChatInfo
{string _name;int _id;string _msg;
};int main()
{ChatInfo winfo { 张三, 123456, 你好 };return 0;
}像这样我们就可以用ostringstream把内容放到一个字符串然后发送给需要这些信息的人别人收到后就可以用istringstream来提取。不过插入的时候每个后面都加上换行或者空格因为提取时默认以换行或者空格来分割。
复杂的实际情况还有其它方法。
CIO流
结束。