網(wǎng)站首頁 編程語言 正文
一、讀寫文本文件
1.1 寫文件
寫文件步驟如下:
-
包含頭文件
#include <fstream>
-
創(chuàng)建流對(duì)象
ofstream ofs;
-
打開文件
ofs.open("文件路徑",打開方式);
-
寫數(shù)據(jù)
ofs << "寫入的數(shù)據(jù)";
-
關(guān)閉文件
ofs.close();
文件打開方式:
打開方式 | 解釋 |
---|---|
ios::in | 為讀文件而打開文件 |
ios::out | 為寫文件而打開文件 |
ios::ate | 初始位置:文件尾 |
ios::app | 追加方式寫文件 |
ios::trunc | 如果文件存在先刪除,再創(chuàng)建 |
ios::binary | 二進(jìn)制方式 |
注意:?文件打開方式可以配合使用,利用|操作符
例如:用二進(jìn)制方式寫文件?ios::binary | ios:: out
示例:
#include <fstream> void test01() { ofstream ofs; ofs.open("test.txt", ios::out); ofs << "姓名:張三" << endl; ofs << "性別:男" << endl; ofs << "年齡:18" << endl; ofs.close(); } int main() { test01(); system("pause"); return 0; }
總結(jié):
- 文件操作必須包含頭文件 fstream
- 讀文件可以利用 ofstream ,或者fstream類
- 打開文件時(shí)候需要指定操作文件的路徑,以及打開方式
- 利用 <<可以向文件中寫數(shù)據(jù)
- 操作完畢,要關(guān)閉文件
1.2讀文件
讀文件與寫文件步驟相似,但是讀取方式相對(duì)于比較多
讀文件步驟如下:
-
包含頭文件
#include <fstream>
-
創(chuàng)建流對(duì)象
ifstream ifs;
-
打開文件并判斷文件是否打開成功
ifs.open("文件路徑",打開方式);
-
讀數(shù)據(jù)
四種方式讀取
-
關(guān)閉文件
ifs.close();
示例:
#include <fstream> #include <string> void test01() { ifstream ifs; ifs.open("test.txt", ios::in); if (!ifs.is_open()) { cout << "文件打開失敗" << endl; return; } //第一種方式 //char buf[1024] = { 0 }; //while (ifs >> buf) //{ // cout << buf << endl; //} //第二種 //char buf[1024] = { 0 }; //while (ifs.getline(buf,sizeof(buf))) //{ // cout << buf << endl; //} //第三種 //string buf; //while (getline(ifs, buf)) //{ // cout << buf << endl; //} char c; while ((c = ifs.get()) != EOF) { cout << c; } ifs.close(); } int main() { test01(); system("pause"); return 0; }
總結(jié):
- 讀文件可以利用 ifstream ,或者fstream類
- 利用is_open函數(shù)可以判斷文件是否打開成功
- close 關(guān)閉文件
二、讀寫二進(jìn)制文件
以二進(jìn)制的方式對(duì)文件進(jìn)行讀寫操作
打開方式要指定為?ios::binary
2.1 寫文件
二進(jìn)制方式寫文件主要利用流對(duì)象調(diào)用成員函數(shù)write
函數(shù)原型 :ostream& write(const char * buffer,int len);
參數(shù)解釋:字符指針buffer指向內(nèi)存中一段存儲(chǔ)空間。len是讀寫的字節(jié)數(shù)
示例:
#include <fstream> #include <string> class Person { public: char m_Name[64]; int m_Age; }; //二進(jìn)制文件 寫文件 void test01() { //1、包含頭文件 //2、創(chuàng)建輸出流對(duì)象 ofstream ofs("person.txt", ios::out | ios::binary); //3、打開文件 //ofs.open("person.txt", ios::out | ios::binary); Person p = {"張三" , 18}; //4、寫文件 ofs.write((const char *)&p, sizeof(p)); //5、關(guān)閉文件 ofs.close(); } int main() { test01(); system("pause"); return 0; }
總結(jié):
- 文件輸出流對(duì)象 可以通過write函數(shù),以二進(jìn)制方式寫數(shù)據(jù)
2.2 讀文件
二進(jìn)制方式讀文件主要利用流對(duì)象調(diào)用成員函數(shù)read
函數(shù)原型:istream& read(char *buffer,int len);
參數(shù)解釋:字符指針buffer指向內(nèi)存中一段存儲(chǔ)空間。len是讀寫的字節(jié)數(shù)
示例:
#include <fstream> #include <string> class Person { public: char m_Name[64]; int m_Age; }; void test01() { ifstream ifs("person.txt", ios::in | ios::binary); if (!ifs.is_open()) { cout << "文件打開失敗" << endl; } Person p; ifs.read((char *)&p, sizeof(p)); cout << "姓名: " << p.m_Name << " 年齡: " << p.m_Age << endl; } int main() { test01(); system("pause"); return 0; }
文件輸入流對(duì)象 可以通過read函數(shù),以二進(jìn)制方式讀數(shù)據(jù)
原文鏈接:https://www.cnblogs.com/wml-it/p/15899355.html
相關(guān)推薦
- 2022-08-20 python操作csv格式文件之csv.DictReader()方法_python
- 2022-08-06 使用Gorm操作Oracle數(shù)據(jù)庫踩坑記錄_Golang
- 2022-12-01 C語言數(shù)據(jù)在內(nèi)存中的存儲(chǔ)流程深入分析_C 語言
- 2022-08-23 C++?primer超詳細(xì)講解順序容器_C 語言
- 2022-05-20 Spring-實(shí)現(xiàn)AOP的三種方式演示
- 2022-04-08 CentOS7上安裝Docker的詳細(xì)步驟_docker
- 2022-05-09 sql語句中union的用法與踩坑記錄_MsSql
- 2022-09-10 Go語言中的IO操作及Flag包的用法_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支