当前位置:网站首页>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下不管是文本模式还是二进制模式都是一样的
边栏推荐
- [batch dos-cmd command - summary and summary] - external command -cmd download command and packet capture command (WGet)
- Change the current count of auto increment values in MySQL- Changing the current count of an Auto Increment value in MySQL?
- 【UVM入门 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常规方法、寄存器模型的应用场景
- Common functions of OrCAD schematic
- Analysis on the trend of the number of national cinemas, film viewers and average ticket prices in 2021 [figure]
- lotus v1.16.0-rc2 Calibration-net
- Editing the date formatting of x-axis tick labels in Matplotlib - editing the date formatting of x-axis tick labels in Matplotlib
- Lotus windowsost manually triggers space-time proof calculation
- 我的处女作杀青啦!
- 关于硬件问题造成的MCU死机,过来人简单的谈一谈
猜你喜欢

ES can finally find brother Wukong!

我的处女作杀青啦!

Enter an integer with any number of bits, and output the sum of each bit of the number. For example: 1234 – > 10

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

Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance

Zhugeliang vs pangtong, taking distributed Paxos

Evolution of Alibaba e-commerce architecture

Common functions of OrCAD schematic

诸葛亮 VS 庞统,拿下分布式 Paxos

We are different
随机推荐
shell小技巧(一百三十四)简单的键盘输入记录器
Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
Rotation vector (rotation matrix) and Euler angle
VectorDraw Developer Framework 10.10
[Yu Yue education] engineering testing technology reference of Wenhua University
Enter an integer with any number of bits, and output the sum of each bit of the number. For example: 1234 – > 10
Lotus windowsost manually triggers space-time proof calculation
What common APIs are involved in thread state changes
我与CSDN的一年时光及大学经验分享
Sqlmap advanced use – cookies
Let's talk about MCU crash caused by hardware problems
Display purchase Summary - Dell 2705qm BenQ pd2700u
Zhugeliang vs pangtong, taking distributed Paxos
Kube scheduler source code analysis (1) - initialization and startup analysis
From perceptron to transformer, a brief history of deep learning
N – simple encoding
Don't you know the evolution process and principle of such a comprehensive redis cluster model?
Ppt template of small fresh open class education courseware
【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)
Keepalived monitors the process and automatically restarts the service process