C++核心编程(四)--文件操作
生活随笔
收集整理的這篇文章主要介紹了
C++核心编程(四)--文件操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
5 文件操作
程序運行時產生的數據都屬于臨時數據,程序一點運行結束,就會被釋放
通過文件可以將數據持久化
C++中對文件操作需要包含頭文件:fstream
文件類型分為兩種:
- 文本文件:文件以文本的ASCII碼形式存儲在計算機中
- 二進制文件:文件以文本的二進制形式存儲在計算機中,用戶一般不能直接讀懂他們
操作文件的三大類
ofstream寫操作ifstream讀操作fstream讀寫操作
5.1 文本文件
5.1.1 寫文件
寫文件步驟如下:
- 包含頭文件
#include <fstream> - 創建流對象
ofstream ofs; - 打開文件
fos.open("file_path", open_type); - 寫數據
ofs << "寫入的數據"; - 關閉文件
ofs.close();
文件打開方式:
| 打開方式 | 解釋 |
|---|---|
| ios::in | 讀 |
| ios::out | 寫 |
| ios::ate | 初試位置:文件尾 |
| ios::app | 追加 |
| ios::trunc | 如果文件存在,先刪除,再創建 |
| ios::binary | 二進制方式 |
注意: 文件打開方式可以配合使用,利用 |操作符
例如:利用二進制方式寫文件 ios::binary | ios::out
文件寫示例:
#include<iostream>
using namespace std;// 文本文件寫文件
// 1. 包含頭文件
#include <fstream>void test01()
{// 2. 創建文件流對象ofstream ofs;// 3. 打開文件ofs.open("test.txt", ios::out);// 4. 寫入內容ofs << "姓名:張三" << endl;ofs << "性別:男" << endl;ofs << "年齡:18" << endl;// 5. 關閉文件ofs.close();
}int main()
{test01();return 0;
}
注意: 文件打開,沒有指定路徑,文件生成在和代碼同在一個文件夾下
5.1.2 讀文件
讀文件和寫文件步驟相似,但是讀取的方式比較多
讀文件步驟如下:
- 包含頭文件
#include <fstream> - 創建流對象
ifstream ifs - 打開文件并判斷文件是否打開成功
fis.open("文件路徑“, 打開方式); - 讀數據
四種方式讀取 - 關閉文件
ifs.close();
示例:
#include<iostream>
using namespace std;
#include<string>// 文本文件讀文件
// 1. 包含頭文件
#include <fstream>void test01()
{// 2. 創建文件流對象ifstream ifs;// 3. 打開文件ifs.open("test.txt", ios::in);// 判斷打開是否成功if (!ifs.is_open()){cout << "文件打開失敗!" << endl;return;}// 4. 讀內容// 第一種://char buf[1034] = { 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) // EOF end of file//{// cout << c;//}// 5. 關閉文件ifs.close();
}int main()
{test01();return 0;
}
5.2 二進制文件
以二進制的方式對文件進行讀寫操作
打開方式為:ios::binary
5.2.1 寫文件
二進制方式寫文件主要利用流對象調用成員函數 write
函數原型:ostream& write(const char * buffer, int len);
參數解釋:字符指針buffer指向內存中一段存儲空間、len是讀寫的字節數
示例:
#include<iostream>
using namespace std;
#include<string>// 包含頭文件
#include <fstream>// 二進制文件,寫文件
class Person
{
public:char name[64]; // 姓名int age; // 年齡
};void test01()
{// 2. 創建文件流對象ofstream ofs;// 也可以用如下寫法// ifstream ifsm("person.txt", ios::out | ios::binary)// 3. 打開文件ofs.open("person.txt", ios::out | ios::binary);// 準備數據Person p = {"張三", 20};// 4. 寫數據ofs.write((const char *)&p, sizeof(Person));// 5. 關閉文件ofs.close();
}int main()
{test01();return 0;
}
5.2.2 讀文件
二進制方式讀文件主要利用流對象調用成員函數read
函數原型:iostream& read(char *buffer, int len);
參數解釋:字符指針buffer指向內存中一段存儲空間、len是讀寫的字節數
示例:
#include<iostream>
using namespace std;
#include<string>// 包含頭文件
#include <fstream>// 二進制文件,讀文件
class Person
{
public:char name[64]; // 姓名int age; // 年齡
};void test01()
{// 2. 創建文件流對象ifstream ifs;// 也可以用如下寫法// ifstream ifsm("person.txt", ios::out | ios::binary)// 3. 打開文件ifs.open("person.txt", ios::in | ios::binary);// 判斷文件是否打開成功if (!ifs.is_open()){cout << "打開文件失敗" << endl;return;}// 準備數據Person p;// 4. 寫數據ifs.read((char *)&p, sizeof(Person));cout << "姓名:" << p.name << endl;cout << "年齡:" << p.age << endl;// 5. 關閉文件ifs.close();
}int main()
{test01();return 0;
}
運行結果:
總結
以上是生活随笔為你收集整理的C++核心编程(四)--文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++核心编程(三)
- 下一篇: docker安装Mysql5.7以及远程