[C++ Core Programming] File Operation (5)

5 File operations

The data generated when the program is running are all temporary data, and will be released once the program ends.

Data can be persisted through files

In C++, file operations need to include header files< fstream >

There are two types of files:

  1. Text files - files are stored in the computer as ASCII codes of text
  2. Binary files - files are stored on the computer in a binary form of text, which is generally not directly readable by the user

There are three main categories of operation files:

  1. ofstream: write operations
  2. ifstream: read operation
  3. fstream : read and write operations

5.1 Text files

5.1.1 Writing files

The steps to write a file are as follows:

1. 包含头文件   

   #include <fstream>

2. 创建流对象  

   ofstream ofs;

3. 打开文件

   ofs.open("文件路径",打开方式);

4. 写数据

   ofs << "写入的数据";

5. 关闭文件

   ofs.close();

File opening method:

open way explain
ios::in open file for reading
ios::out open file for writing
ios::ate Initial position: end of file
ios::app write file append
ios::trunc If the file exists, delete it first, then create it
ios::binary binary mode

Note: The file opening method can be used in conjunction with the | operator

For example : write a file in binaryios::binary | ios:: out

Example:

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

Summarize:

  • File operations must include the header file fstream
  • Reading files can use ofstream, or the fstream class
  • When opening a file, you need to specify the path of the operation file and the opening method.
  • Use << to write data to a file
  • When the operation is complete, close the file

5.1.2 Reading files

The steps of reading a file are similar to that of writing a file, but the reading method is relatively more

The steps to read the file are as follows:

1. 包含头文件   

   #include <fstream>

2. 创建流对象  

   ifstream ifs;

3. 打开文件并判断文件是否打开成功

   ifs.open("文件路径",打开方式);

4. 读数据

   四种方式读取

5. 关闭文件

   ifs.close();

Example:

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

Summarize:

  • Reading files can use ifstream, or the fstream class
  • Use the is_open function to determine whether the file is opened successfully
  • close close the file

5.2 Binaries

Read and write files in binary

Open with to be specified asios::binary

5.2.1 Writing files

Writing files in binary mode mainly uses the stream object to call the member function write

Function prototype:ostream& write(const char * buffer,int len);

Parameter explanation : The character pointer buffer points to a storage space in memory. len is the number of bytes read and written

Example:

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

Summarize:

  • The file output stream object can write data in binary mode through the write function

5.2.2 Reading files

Reading files in binary mode mainly uses the stream object to call the member function read

Function prototype:istream& read(char *buffer,int len);

Parameter explanation: The character pointer buffer points to a storage space in memory. len is the number of bytes read and written

Example:

#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;
}
  • The file input stream object can read data in binary mode through the read function

insert image description here


come on!

grateful!

effort!

Guess you like

Origin blog.csdn.net/qq_46092061/article/details/123445567
Recommended