1. 程式人生 > 其它 >C++讀寫檔案操作

C++讀寫檔案操作

一、讀寫文字檔案

1.1 寫檔案

寫檔案步驟如下:

  1. 包含標頭檔案

    #include <fstream>

  2. 建立流物件

    ofstream ofs;

  3. 開啟檔案

    ofs.open("檔案路徑",開啟方式);

  4. 寫資料

    ofs << "寫入的資料";

  5. 關閉檔案

    ofs.close();

檔案開啟方式:

開啟方式 解釋
ios::in 為讀檔案而開啟檔案
ios::out 為寫檔案而開啟檔案
ios::ate 初始位置:檔案尾
ios::app 追加方式寫檔案
ios::trunc 如果檔案存在先刪除,再建立
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;
}

總結:

  • 檔案操作必須包含標頭檔案 fstream
  • 讀檔案可以利用 ofstream ,或者fstream類
  • 開啟檔案時候需要指定操作檔案的路徑,以及開啟方式
  • 利用 <<可以向檔案中寫資料
  • 操作完畢,要關閉檔案

1.2讀檔案

讀檔案與寫檔案步驟相似,但是讀取方式相對於比較多

讀檔案步驟如下:

  1. 包含標頭檔案

    #include <fstream>

  2. 建立流物件

    ifstream ifs;

  3. 開啟檔案並判斷檔案是否開啟成功

    ifs.open("檔案路徑",開啟方式);

  4. 讀資料

    四種方式讀取

  5. 關閉檔案

    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;
}

總結:

  • 讀檔案可以利用 ifstream ,或者fstream類
  • 利用is_open函式可以判斷檔案是否開啟成功
  • close 關閉檔案

二、讀寫二進位制檔案

以二進位制的方式對檔案進行讀寫操作

開啟方式要指定為 ios::binary

2.1 寫檔案

二進位制方式寫檔案主要利用流物件呼叫成員函式write

函式原型 :ostream& write(const char * buffer,int len);

引數解釋:字元指標buffer指向記憶體中一段儲存空間。len是讀寫的位元組數

示例:

#include <fstream>
#include <string>

class Person
{
public:
	char m_Name[64];
	int m_Age;
};

//二進位制檔案  寫檔案
void test01()
{
	//1、包含標頭檔案

	//2、建立輸出流物件
	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;
}

總結:

  • 檔案輸出流物件 可以通過write函式,以二進位制方式寫資料

2.2 讀檔案

二進位制方式讀檔案主要利用流物件呼叫成員函式read

函式原型:istream& read(char *buffer,int len);

引數解釋:字元指標buffer指向記憶體中一段儲存空間。len是讀寫的位元組數

示例:

#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函式,以二進位制方式讀資料

本文來自部落格園,作者:農碼一生,轉載請註明原文連結:https://www.cnblogs.com/wml-it/p/15899355.html


技術的發展日新月異,隨著時間推移,無法保證本部落格所有內容的正確性。如有誤導,請大家見諒,歡迎評論區指正!
個人開原始碼連結:
GitHub:https://github.com/ITMingliang
Gitee:https://gitee.com/mingliang_it
GitLab:https://gitlab.com/ITMingliang
進開發學習交流群: