当前位置:网站首页>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下不管是文本模式还是二进制模式都是一样的
边栏推荐
- 为什么要“除夕”,原来是内存爆了!
- MySQL(十二)——更改表的备注
- How to get the difference between two dates rounded to hours
- LabVIEW generate application (exe) and installer
- SQL solve select basic statement
- Vscode official configuration synchronization scheme
- Rotation vector (rotation matrix) and Euler angle
- [batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)
- Cocos学习日记3——api获取节点、组件
- Flexbox on ie11: stretching images for no reason- Flexbox on IE11: image stretched for no reason?
猜你喜欢

Escape analysis of 982 golang

MCU IO explanation (pull-up pull-down quasi bidirectional input / output push-pull open drain)

PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering

线程状态变化涉及哪些常用 API

【UVM入門 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常規方法、寄存器模型的應用場景
![Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]](/img/ea/3eba7e4a433b0c501f9b207641dc6a.jpg)
Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]

Kube scheduler source code analysis (1) - initialization and startup analysis

Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes

VectorDraw Developer Framework 10.10

One year's time and University experience sharing with CSDN
随机推荐
Global variables & local variables
Path planner based on time potential function in dynamic environment
Using awk to process input from multiple files
[XXL job] the pond is green and the wind is warm. I remember that Yu Zhen first met
lotus v1.16.0-rc3 calibnet
LabVIEW jump to web page
MySQL - definition and assignment of variables
LTpowerCAD II和LTpowerPlanner III
48 pictures | teach you the performance monitoring, pressure testing and tuning of microservices by hand
LabVIEW generate application (exe) and installer
Shell命令学习
shell小技巧(一百三十四)简单的键盘输入记录器
Cocos learning diary 3 - API acquisition nodes and components
Loopholes in the missed scanning system of Lvmeng and its repair scheme
VectorDraw Developer Framework 10.10
Explain distributed raft with dynamic diagram
How do I know if mysqlnd is an active driver- How to know if MySQLnd is the active driver?
Flexbox on ie11: stretching images for no reason- Flexbox on IE11: image stretched for no reason?
【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
I have used it for six years!