当前位置:网站首页>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下不管是文本模式还是二进制模式都是一样的
边栏推荐
- How do I know if mysqlnd is an active driver- How to know if MySQLnd is the active driver?
- 正版photoshop2022购买体验经历分享
- This year, I graduated
- Debian introduction
- [Batch dos - cmd Command - Summary and Summary] - External Command - cmd Download Command, wget Command
- Loopholes in the missed scanning system of Lvmeng and its repair scheme
- 几款不错的天气插件
- 高数基础_函数的奇偶性
- Finally, when you open source the applet ~
- Zhugeliang vs pangtong, taking distributed Paxos
猜你喜欢
14 BS object Node name Name attrs string get node name attribute content
数据可视化没有重点怎么办?
Authentique Photoshop 2022 expérience d'achat partage
[leetcode] two num · sum of two numbers
The principle of Zener diode, what is its function?
JMeter introduction practice ----- use of global variables and local variables
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
Design a MySQL table for message queue to store message data
用太极拳讲分布式理论,真舒服!
Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
随机推荐
【UVM入门 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常规方法、寄存器模型的应用场景
SQL query, if value is null then return 1 - SQL query, if value is null then return 1
【批处理DOS-CMD命令-汇总和小结】-文件与目录操作命令(md、rd、xcopy、dir、cd、set、move、copy、del、type、sort)
Global variables & local variables
Can we use function pointers in go- Can we have function pointers in Go?
How to get the difference between two dates rounded to hours
Escape analysis of 982 golang
LabVIEW jump to web page
Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
Cocos learning diary 3 - API acquisition nodes and components
Authentique Photoshop 2022 expérience d'achat partage
5g private network market is in full swing, and it is crucial to solve deployment difficulties in 2022
This year, I graduated
【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
Change the current count of auto increment values in MySQL- Changing the current count of an Auto Increment value in MySQL?
From perceptron to transformer, a brief history of deep learning
线程状态变化涉及哪些常用 API
三年营收连续下滑,天地壹号困在醋饮料里
Explain distributed raft with dynamic diagram
MySQL(十二)——更改表的备注