網(wǎng)站首頁 編程語言 正文
一、讀寫文本文件
1.1 寫文件
寫文件步驟如下:
-
包含頭文件
#include <fstream>
-
創(chuàng)建流對象
ofstream ofs;
-
打開文件
ofs.open("文件路徑",打開方式);
-
寫數(shù)據(jù)
ofs << "寫入的數(shù)據(jù)";
-
關閉文件
ofs.close();
文件打開方式:
打開方式 | 解釋 |
---|---|
ios::in | 為讀文件而打開文件 |
ios::out | 為寫文件而打開文件 |
ios::ate | 初始位置:文件尾 |
ios::app | 追加方式寫文件 |
ios::trunc | 如果文件存在先刪除,再創(chuàng)建 |
ios::binary | 二進制方式 |
注意:?文件打開方式可以配合使用,利用|操作符
例如:用二進制方式寫文件?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ù)據(jù)
- 操作完畢,要關閉文件
1.2讀文件
讀文件與寫文件步驟相似,但是讀取方式相對于比較多
讀文件步驟如下:
-
包含頭文件
#include <fstream>
-
創(chuàng)建流對象
ifstream ifs;
-
打開文件并判斷文件是否打開成功
ifs.open("文件路徑",打開方式);
-
讀數(shù)據(jù)
四種方式讀取
-
關閉文件
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 關閉文件
二、讀寫二進制文件
以二進制的方式對文件進行讀寫操作
打開方式要指定為?ios::binary
2.1 寫文件
二進制方式寫文件主要利用流對象調(diào)用成員函數(shù)write
函數(shù)原型 :ostream& write(const char * buffer,int len);
參數(shù)解釋:字符指針buffer指向內(nèi)存中一段存儲空間。len是讀寫的字節(jié)數(shù)
示例:
#include <fstream> #include <string> class Person { public: char m_Name[64]; int m_Age; }; //二進制文件 寫文件 void test01() { //1、包含頭文件 //2、創(chuàng)建輸出流對象 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、關閉文件 ofs.close(); } int main() { test01(); system("pause"); return 0; }
總結(jié):
- 文件輸出流對象 可以通過write函數(shù),以二進制方式寫數(shù)據(jù)
2.2 讀文件
二進制方式讀文件主要利用流對象調(diào)用成員函數(shù)read
函數(shù)原型:istream& read(char *buffer,int len);
參數(shù)解釋:字符指針buffer指向內(nèi)存中一段存儲空間。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; }
文件輸入流對象 可以通過read函數(shù),以二進制方式讀數(shù)據(jù)
原文鏈接:https://www.cnblogs.com/wml-it/p/15899355.html
相關推薦
- 2022-07-20 C語言實例講解嵌套語句的用法_C 語言
- 2022-07-19 AI與Python人工智能啟發(fā)式搜索概念理解_python
- 2022-08-11 boost.asio框架系列之socket編程_C 語言
- 2022-06-12 如何在React項目中使用AntDesign_React
- 2022-04-20 超全整理visual?studio快捷鍵使用技巧_相關技巧
- 2022-04-06 Android中卡頓優(yōu)化布局詳細介紹_Android
- 2022-12-09 OpenCV?imread讀取圖片失敗的問題及解決_python
- 2022-09-26 python中創(chuàng)建一個包并引用使用的操作方法_python
- 最近更新
-
- 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之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支