当前位置:网站首页>STL教程4-输入输出流和对象序列化
STL教程4-输入输出流和对象序列化
2022-06-25 06:39:00 【贪睡的蜗牛】
标准I/O=标准输入+标准输出
文件I/O=文件输入+文件输出
一、标准输入输出流

cout是全局对象,已经和显示屏关联
cerr没有缓冲区,clog有缓冲区,cin和cout也有缓冲区
cin.peek();偷窥下缓冲区,并不从缓冲区拿走
cin.putback()用是将前面用get或者getline函数从输入流中读取的字符ch返回到输入流,插入到当前指针的位置
cout<<flush()//刷新缓冲区
cout.put(‘a’).put(‘a’);//输出一个字符并支持链式编程
cout.write(“aaa”,strlen(“aaa”));
格式化输出
1、成员方法的方式
代码
void test06() {
int number = 10;
//成员方法的模式
cout.unsetf(ios::dec);//卸载掉默认的10进制输出方式
cout.setf(ios::oct);//设置为八进制输出
cout << number<<endl;
cout.setf(ios::showbase);//将隐藏的一些内容也显示出来
cout << number<<endl;
cout.unsetf(ios::oct);//卸载八进制
cout.setf(ios::hex);//设置为十六进制
cout << number << endl;
cout.width(10);//设置位宽
cout.fill('*');//设置内容填充
cout << number << endl;
cout.setf(ios::left);//设置左对齐
cout << number << endl;
}
2、通过控制符
需要包含头文件#include 
int number2 = 10;
cout << hex //设置为16进制
<< resetiosflags(ios::showbase)//设置显示符号位
<< setw(10)//设置宽度为10
<< setfill('$')//设置填充为$
<<setiosflags(ios::left)
<< number2
<< endl;
二、文件操作
文件读取
需要包含头文件#include < fstream >//文件读写
定义文件对象有两种方法,
1、ifstream ism(filePath,ios::in);
2、ifstream ism;
ism.open(filePath, ios::in);
const char* filePath = "C:\\Users\\admin\\Desktop\\source.txt";
ifstream ism(filePath,ios::in);//只读方式打开文件
//或者定义一个对象,以对象的成员方法打开文件
/*ifstream ism; ism.open(filePath, ios::in);*/
//这里以对象为判断说明肯定重载了
if (!ism) {
cout << "打开文件失败" << endl;
}
//读文件 由于建立了管道了
char ch;
while ( ism.get(ch))
{
cout << ch;
}
//用完了关闭文件
ism.close();
文件写入
定义文件写入对象和定义文件读取格式一样
ofstream osm(targetPath, ios::out | ios::app); //仅有out是覆盖模式,加上后面| ios::app是追加模式
在后面osm.put(ch);往里添加元素
void test07() {
const char* filePath = "C:\\Users\\admin\\Desktop\\source.txt";
const char* targetPath = "C:\\Users\\admin\\Desktop\\target.txt";
ifstream ism(filePath,ios::in);//只读方式打开文件
ofstream osm(targetPath, ios::out | ios::app); //仅有out是覆盖模式,加上后面是追加模式
//或者定义一个对象,以对象的成员方法打开文件
/*ifstream ism; ism.open(filePath, ios::in);*/
//这里以对象为判断说明肯定重载了
if (!ism) {
cout << "打开文件失败" << endl;
}
//读文件 由于建立了管道了
char ch;
while ( ism.get(ch))
{
cout << ch;
osm.put(ch);
}
//用完了关闭文件
ism.close();
osm.close();
}
二进制模式读写
新建一个Person类 将p1和p2写入文件中 p1和p2是以二进制形式存储的,上面说的普通的文本模式读取其实也是二进制形式存储的
即使乱码也是二进制形式存储的 window上以回车和换行即/r/n两个符号作为行结束的标志 linux上仅换行作为换行/n标志
以文本模式读取文件,会自动的将每行的/r/n换成/n,写文件会将/n换成/r/n
但是在linxu下不管是文本模式还是二进制模式都是一样的




总结
文件都是以二进制模式存储的,即使在内存里新建的对象也是二进制形式,这时就可以将对象写到文件里,而文本模式打开文件和二进制模式打开文件的区别在于,window上以回车和换行即/r/n两个符号作为行结束的标志,linux上仅换行作为换行/n标志 。以文本模式读取文件,会自动的将每行的/r/n换成/n,写文件会将/n换成/r/n,但是在linux下不管是文本模式还是二进制模式都是一样的
边栏推荐
- Zhugeliang vs pangtong, taking distributed Paxos
- What is the difference between norflash and nandflash
- 关于硬件问题造成的MCU死机,过来人简单的谈一谈
- Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
- Distributed quorum NWR of the alchemy furnace of the Supreme Master
- 三年营收连续下滑,天地壹号困在醋饮料里
- VectorDraw Web Library 10.10
- 韩信大招:一致性哈希
- [Batch dos - cmd Command - Summary and Summary] - cmd extension Command, extension Function (CMD / E: on, CMD / E: off)
- 14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
猜你喜欢
![[batch dos-cmd command - summary and summary] - external command -cmd download command and packet capture command (WGet)](/img/00/5a5b081b78ad6a6c1c3a3c847dd315.png)
[batch dos-cmd command - summary and summary] - external command -cmd download command and packet capture command (WGet)

【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)

VectorDraw Web Library 10.10

MySQL facet 01

Distributed quorum NWR of the alchemy furnace of the Supreme Master

鸿蒙页面菜单的选择

One year's time and University experience sharing with CSDN

【pytest】修改allure报告中的logo及参数化

威迈斯新能源冲刺科创板:年营收17亿 应收账款账面价值近4亿

Path planner based on time potential function in dynamic environment
随机推荐
太上老君的炼丹炉之分布式 Quorum NWR
稳压二极管的原理,它有什么作用?
[XXL job] the pond is green and the wind is warm. I remember that Yu Zhen first met
Redirect to previous page after login? PHP - Redirecting to previous page after login? PHP
Ppt template of small fresh open class education courseware
[Yu Yue education] engineering testing technology reference of Wenhua University
Common functions of OrCAD schematic
Analysis on the trend of the number of national cinemas, film viewers and average ticket prices in 2021 [figure]
高考志愿填报,为啥专业最后考虑?
313. Binary sum
Domestic MCU perfectly replaces STM chip model of Italy France
图扑软件数字孪生 3D 风电场,智慧风电之海上风电
Rotation vector (rotation matrix) and Euler angle
What is the new business model of Taishan crowdfunding in 2022?
14 BS object Node name Name attrs string get node name attribute content
指南针可以开股票账户吗?安全吗?
Flexbox on ie11: stretching images for no reason- Flexbox on IE11: image stretched for no reason?
Authentique Photoshop 2022 expérience d'achat partage
Conditional grouping with $exists inside $cond
PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering