当前位置:网站首页>STL tutorial 4- input / output stream and object serialization
STL tutorial 4- input / output stream and object serialization
2022-06-25 07:41:00 【Sleepy snail】
standard I/O= The standard input + standard output
file I/O= File input + File output
One 、 Standard I / O stream

cout Global object , Has been associated with the display 
cerr There is no buffer ,clog There are buffers ,cin and cout There are also buffers
cin.peek(); Peeping into the buffer , Does not take from the buffer
cin.putback() Use is to use the front get perhaps getline Function to read characters from the input stream ch Return to the input stream , Insert to current pointer position
cout<<flush()// Refresh buffer
cout.put(‘a’).put(‘a’);// Output a character and support chain programming
cout.write(“aaa”,strlen(“aaa”));
Format output
1、 The way member methods 
Code
void test06() {
int number = 10;
// The pattern of member methods
cout.unsetf(ios::dec);// Uninstall the default 10 Binary output mode
cout.setf(ios::oct);// Set to octal output
cout << number<<endl;
cout.setf(ios::showbase);// Show some hidden contents
cout << number<<endl;
cout.unsetf(ios::oct);// Unload octal
cout.setf(ios::hex);// Set to hexadecimal
cout << number << endl;
cout.width(10);// Set bit width
cout.fill('*');// Set content fill
cout << number << endl;
cout.setf(ios::left);// Set left alignment
cout << number << endl;
}
2、 Through the controller
Need to include header file #include 
int number2 = 10;
cout << hex // Set to 16 Base number
<< resetiosflags(ios::showbase)// Set the display symbol bit
<< setw(10)// Set the width to 10
<< setfill('$')// Set the padding to $
<<setiosflags(ios::left)
<< number2
<< endl;
Two 、 File operations
File read
Need to include header file #include < fstream >// File read and write
There are two ways to define file objects ,
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);// Open the file in read-only mode
// Or define an object , Open the file as a member of the object
/*ifstream ism; ism.open(filePath, ios::in);*/
// Here, the object must be overloaded
if (!ism) {
cout << " fail to open file " << endl;
}
// Reading documents Thanks to the establishment of the pipeline
char ch;
while ( ism.get(ch))
{
cout << ch;
}
// Finished closing files
ism.close();
File is written to
Defining the file write object is the same as defining the file read format
ofstream osm(targetPath, ios::out | ios::app); // have only out It's overlay mode , Add the back | ios::app It's the append mode
rearwards osm.put(ch); Add elements to it
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);// Open the file in read-only mode
ofstream osm(targetPath, ios::out | ios::app); // have only out It's overlay mode , Plus the following is the append mode
// Or define an object , Open the file as a member of the object
/*ifstream ism; ism.open(filePath, ios::in);*/
// Here, the object must be overloaded
if (!ism) {
cout << " fail to open file " << endl;
}
// Reading documents Thanks to the establishment of the pipeline
char ch;
while ( ism.get(ch))
{
cout << ch;
osm.put(ch);
}
// Finished closing files
ism.close();
osm.close();
}
Binary mode read / write
Create a new one Person class take p1 and p2 Write to file p1 and p2 Is stored in binary form , The common text mode reading mentioned above is actually stored in binary form
Even garbled code is stored in binary form window Go to carriage return and line feed /r/n Two symbols are used to mark the end of the line linux Just wrap on as a newline /n sign
Read files in text mode , Will automatically change the /r/n Switch to /n, Writing a file will /n Switch to /r/n
But in linxu It is the same in both text mode and binary mode




summary
Files are stored in binary mode , Even newly created objects in memory are in binary form , Then you can write the object to the file , The difference between opening files in text mode and binary mode is ,window Go to carriage return and line feed /r/n Two symbols are used to mark the end of the line ,linux Just wrap on as a newline /n sign . Read files in text mode , Will automatically change the /r/n Switch to /n, Writing a file will /n Switch to /r/n, But in linux It is the same in both text mode and binary mode
边栏推荐
- 【批处理DOS-CMD命令-汇总和小结】-外部命令-cmd下载命令、抓包命令(wget)
- Tempest HDMI leak receive 2
- TEMPEST HDMI泄漏接收 1
- Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing
- IAR compiler flashback
- 【批处理DOS-CMD命令-汇总和小结】-文件与目录操作命令(md、rd、xcopy、dir、cd、set、move、copy、del、type、sort)
- C#入门教程
- 一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
- Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w
- [batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)
猜你喜欢
![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]

三年营收连续下滑,天地壹号困在醋饮料里

Ns32f103c8t6 can perfectly replace stm32f103c8t6

Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing

函数模板_类模板

14 BS object Node name Name attrs string get node name attribute content

13 `bs_duixiang.tag标签`得到一个tag对象

China Mobile MCU product information

Vscode official configuration synchronization scheme

Selection of Hongmeng page menu
随机推荐
音频(五)音频特征提取
点云智绘在智慧工地中的应用
太上老君的炼丹炉之分布式 Quorum NWR
【LeetCode】two num·两数之和
The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
Hanxin's trick: consistent hashing
用太极拳讲分布式理论,真舒服!
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
College entrance examination voluntary filling, why is the major the last consideration?
函数模板_类模板
國外LEAD域名郵箱獲取途徑
Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection
Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
C#入门教程
【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)
MySQL face Scripture eight part essay
C Getting Started tutorial
Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
useMemo模拟useCallback