天天看點

C++核心程式設計---5. 檔案操作【P143~P146】5. 檔案操作

C++核心程式設計---5. 檔案操作【P143~P146】

  • 5. 檔案操作
    • 5.1 文本檔案-寫檔案
    • 5.2 文本檔案-讀檔案
    • 5.3 二進制檔案-寫檔案
    • 5.4 二進制檔案-讀檔案

5. 檔案操作

程式運作時産生的資料都屬于臨時資料,程式一旦運作結束都會被釋放通過檔案可以将資料持久化

C++中對檔案操作需要包含頭檔案 < fstream >

檔案類型分為兩種:

文本檔案:檔案以文本的ASCII碼形式存儲在計算機中

二進制檔案:檔案以文本的二進制形式存儲在計算機中,使用者一般不能直接讀懂它們

操作檔案的三大類:

  • ofstream:寫操作
  • ifstream:讀操作
  • fstream:讀寫操作

5.1 文本檔案-寫檔案

寫檔案步驟如下:

  • 1、包含頭檔案

    #include< fstream >

  • 2、建立流對象

    ofstream ofs;

  • 3、打開檔案

    ofs.open(“檔案路徑”,打開方式);

  • 4、寫資料

    ofs << “寫入的資料”;

  • 5、關閉檔案

    ofs.close();

檔案打開方式:

C++核心程式設計---5. 檔案操作【P143~P146】5. 檔案操作

注意:檔案打開方式可以配合使用,利用 | 操作符;

例如:用二進制方式寫檔案 ios::binary | ios::out

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

void test01()
{
	//1、包含頭檔案 fstream
	//2、建立流對象
	fstream ofs;
	//3、指定打開方式
	ofs.open("test.txt", ios::out);//如果不指定檔案路徑,那麼生成的檔案路徑會和項目的路徑保持一緻
	//4、寫内容
	ofs << "河北工業大學 to 南開大學" << endl;
	ofs << "河北工業大學 to 南開大學" << endl;
	ofs << "河北工業大學 to 南開大學" << endl;
	ofs << "河北工業大學 to 南開大學" << endl;
	ofs << "河北工業大學 to 南開大學" << endl;
	//5、關閉檔案
	ofs.close();
}

int main()
{
	test01();

	system("pause");
	return 0;
}
           

5.2 文本檔案-讀檔案

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

讀檔案步驟如下:

  • 1.包含頭檔案

    #include < fstream >

  • 2.建立流對象

    ifstream ifs;

  • 3.打開檔案并判斷檔案是否打開成功

    ifs.open(“檔案路徑”,打開方式);

  • 4.讀資料

    四種方式讀取

  • 5.關閉檔案

    ifs.close();

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

void test01()
{
	//1、包含頭檔案 fstream
	//2、建立流對象
	ifstream ifs;
	//3、打開檔案并且判斷是否打開成功
	ifs.open("test.txt", ios::in);

	if (!ifs.is_open())
	{
		cout << "檔案打開失敗" << endl;
		return;
	}
	//4、讀資料(4種方式)
	
	方式1
	//char buf[1024] = { 0 };//建立一個字元數組,把讀到的資料都放在這個數組裡邊
	//while (ifs >> buf)
	//{
	//	cout << buf << endl;
	//}

	方式2
	//char buf[1024] = { 0 };
	//while (ifs.getline(buf,sizeof(buf)))
	//{
	//	cout << buf << endl;
	//}

	方式3
	//string buf;
	//while (getline(ifs, buf))
	//{
	//	cout << buf << endl;
	//}

	//方式4
	char c;
	while ((c = ifs.get()) != EOF)//EOF:end of file
	{
		cout << c;
	}

	//5、關閉檔案
	ifs.close();
}

int main()
{
	test01();

	system("pause");
	return 0;
}
           

5.3 二進制檔案-寫檔案

二進制方式寫檔案主要利用流對象調用成員函數 write

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

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

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

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

void test01()
{
	//1、包含頭檔案
	//2、建立流對象
	ofstream ofs;
	//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函數,以二進制方式寫資料。

5.4 二進制檔案-讀檔案

二進制方式讀檔案主要利用流對象調用成員函數read

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

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

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

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

void test01()
{
	//1、包含頭檔案
	//2、建立流對象
	ifstream ifs;
	//3、打開檔案  判斷檔案是否打開成功
	ifs.open("person.txt", ios::in | ios::binary);
	if (!ifs.is_open())
	{
		cout << "檔案打開失敗" << endl;
		return;
	}
	//4、讀檔案
	Person p;
	ifs.read((char *)&p, sizeof(Person));
	cout << "姓名:" << p.m_Name << "    年齡:" << p.m_Age << endl;
	//5、關閉檔案
	ifs.close();
}

int main()
{
	test01();

	system("pause");
	return 0;
}
           

繼續閱讀