網站首頁 編程語言 正文
1 輸入輸出IO流
1.1 圖解輸入輸出流
IO設備:文件、終端(dos黑框框)、特殊的數(shù)據(jù)類型(streamstring)
1.2 輸入輸出流類庫
C++中的輸入輸出流是靠定義好的類庫來操作的
2 文件讀寫操作
2.1 文件的打開方式
2.2 文件讀寫類庫的頭文件
頭文件:fstream
ofstream:讀寫
istream:讀操作
ofstream:寫操作
2.3 文本文件讀寫
使用ofstream來寫文本
ofstream寫入文件默認打開方式是ios::trunc,即沒有文件那么創(chuàng)建,該文件存在并且有內容會直接清空內容
#include<iostream>
#include<windows.h>
#include<fstream>
using namespace std;
int main() {
ofstream outfile;
string name;
int age;
cin >> name >> age;
outfile.open("C:/Users/98207/desktop/test.txt", ios::out); // 寫入文件,沒有文件會新建
outfile << name << endl;
outfile << age;
outfile.close(); // 文件結束需要關閉
return 0;
}
使用ifstream讀取文件
程序:
#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using namespace std;
int main() {
ifstream infile;
string str;
int age;
infile.open("C:/Users/98207/desktop/test.txt", ios::in); // 讀取文件
while (1) {
if (infile.eof()) {
break;
}
infile >> str;
cout << str << endl;;
// getline(infile, str);
// cout << str << endl;
}
infile .close();
return 0;
}
結果:
bian
12
使用fstream來讀寫文件
寫入文件fstream默認不會截斷文件
#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using namespace std;
int main() {
string name;
int age;
fstream outfile;
outfile.open("C:/Users/98207/Desktop/test2.txt", ios::out);
cin >> name >> age;
outfile << name << endl;
outfile << age;
outfile.close();
return 0;
}
讀取文件
#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using namespace std;
int main() {
string str;
fstream infile;
infile.open("C:/Users/98207/Desktop/test2.txt", ios::in);
while (1) {
if (infile.eof()) {
break;
}
infile >> str;
cout << str << endl;
}
infile.close();
return 0;
}
2.4 二進制的讀寫
二進制和文本寫區(qū)別在于數(shù)字,二進制數(shù)字是把實際字節(jié)數(shù)寫入進去。
比如整數(shù)9,那么寫入的是4個char字符0009,至于存儲的大小端方式要看cpu。
2.4.1 二進制寫
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
fstream outfile;
char name[20];
int age;
// 為什么保存格式是dat,因為使用文本格式會按照文本格式解析,最后出來的是亂碼
outfile.open("C:/Users/98207/Desktop/1.dat", ios::trunc | ios::out | ios::binary);
cin >> name >> age;
outfile << name << '\t';
outfile.write((char*)&age, sizeof(age)); // 二進制寫
outfile.close();
return 0;
}
2.4.2 二進制讀
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
fstream infile;
char name[20];
char temp;
int age;
infile.open("C:/Users/98207/Desktop/1.dat", ios::in | ios::binary);
infile >> name;
infile.read((char*)&temp, sizeof(temp)); // 丟棄制表符
infile.read((char*)&age, sizeof(age));
cout << name << '\t' << age << endl;
infile.close();
return 0;
}
2.5 按照特殊格式讀寫
2.5.1 特殊格式寫入
#include<iostream>
#include<fstream> //ofstream
#include<sstream> // stringstream
using namespace std;
int main() {
stringstream ret;
ofstream outfile;
string name;
int age;
outfile.open("C:/Users/98207/Desktop/test2.txt", ios::out | ios::trunc);
while (1) {
cin >> name >> age;
if (cin.eof()) {
break;
}
ret << name << "\t\t\t" << age << endl; // ret會累積
// outfile << ret.str();
// ret.clear();
}
outfile << ret.str();
outfile.close();
return 0;
}
2.5.2 特殊格式讀取
#include<iostream>
#include<fstream>
#include<string> // getline, string
using namespace std;
int main() {
fstream infile;
string str;
char name[20];
int age;
infile.open("C:/Users/98207/Desktop/test2.txt", ios::in);
while (1) {
getline(infile, str);
if (infile.eof()) {
break;
}
sscanf_s(str.c_str(), "%s %d", name, sizeof(name), & age); // 這里的參數(shù)只能是char類型,這里的空格會替換文件的制表符或者空格
cout << name << "\t\t\t" << age << endl;
}
infile.close();
return 0;
}
2.6 文件流標志
這里常用的就是is_open()和eof()
2.7 文件指針
輸入流指針seekg
原形:basic_istream& seekg( off_type off, // 偏移量
std::ios_base::seekdir dir); // 起始位置
作用:設置輸入流位置
參數(shù)1:偏移量
參數(shù)2:相對位置
- beg 相對于開始位置
- cur 相對于當前位置
- end 相對于結束位置
從開始位置文件指針偏移5個字節(jié),然后讀取內容
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
// ofstream infile;
ifstream infile;
string str;
infile.open("C:/Users/98207/Desktop/1.txt", ios::in);
infile.seekg(5, ios::beg); // 從開始位置偏移5個字節(jié)
while (1) {
getline(infile, str);
cout << str;
if (infile.eof()) {
break;
}
}
infile.close();
return 0;
}
輸入流指針tellg
作用:返回輸入流的當前位置(距離文件的起始位置的偏移量)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
ifstream infile;
string str;
infile.open("C:/Users/98207/Desktop/1.txt", ios::in);
infile.seekg(5, ios::beg); // 設置偏移量位5個字節(jié)
cout << "文件指針偏移量:" << infile.tellg() << endl; // 相對于文件起始位置
infile.close();
return 0;
}
結果:
文件指針偏移量:5
E:\Microsoft Visual Studio\code\Project15\x64\Debug\Project15.exe (進程 68440)已退出,代碼為 0。
要在調試停止時自動關閉控制臺,請啟用“工具”->“選項”->“調試”->“調試停止時自動關閉控制臺”。
按任意鍵關閉此窗口. . .
輸出流指針seekp
作用:設置輸出流位置
函數(shù)原形:basic_ostream& seekp( off_type off, // 偏移量
std::ios_base::seekdir dir ); // 起始位置
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ofstream outfile;
outfile.open("user1.txt", ios::out | ios::trunc);
outfile << "123456789";
outfile.seekp(3, ios::beg); // 指針先指向開頭,然后向后偏移三個字節(jié)
outfile << "ABC";
outfile.close();
return 0;
}
原文鏈接:https://blog.csdn.net/qq_14824921/article/details/127495120
相關推薦
- 2022-11-06 python中defaultdict用法實例詳解_python
- 2023-03-18 RedisTemplate訪問Redis的更好方法_Redis
- 2022-06-27 Python查找多個字典公共鍵key的方法_python
- 2022-10-01 Iptables防火墻limit模塊擴展匹配規(guī)則詳解_安全相關
- 2023-10-17 element ui的from表單,不斷修改版
- 2022-09-03 Matplotlib中文亂碼的兩種詳細解決方案_python
- 2022-09-13 C語言創(chuàng)建數(shù)組實現(xiàn)函數(shù)init,empty,reverse_C 語言
- 2022-07-29 使用React?Router?v6?添加身份驗證的方法_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支