[C++ core] detailed explanation of file operation

1. File operation

The data generated when the program is running is all temporary data, and will be released once the program finishes running, and the data can be persisted through files . There are two file types:

  1. Text files - files are stored on your computer as text in ASCII
  2. Binary files - files are stored on the computer in binary form of text, and users generally cannot read them directly

Three categories of operating files:

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

File operations in C++ need to include header files< fstream >

1.1 Text files

1.1.1 Writing files

The steps to write a file are as follows:

  1. Include the header file:#include <fstream>

  2. Create a stream object:ofstream ofs;

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

  4. Write data:ofs << "写入的数据";

  5. Close the file:ofs.close();

File open method:

open method explain
ios::in open file for reading
ios::out open file for writing
ios::ate Initial position: end of file
ios::app Appending to write files
ios::trunk 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 binary modeios::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();

	return 0;
}

Summarize:

  • File operations must include the header file fstream
  • Read files can use ofstream, or 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 the file
  • After the operation is complete, close the file

1.1.2 Reading files

The steps of reading a file are similar to those of writing a file, but there are more reading methods

The steps to read a file are as follows:

  1. Include the header file:#include <fstream>

  2. Create a stream object:ifstream ifs;

  3. Open the file and judge whether the file is opened successfully:ifs.open("文件路径",打开方式);

  4. Read data: read in four ways (such as the following code case)

  5. Close the file: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();

	return 0;
}

Summarize:

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

1.2 Binaries

To read and write files in binary mode, the opening method should be specified asios::binary

1.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();

	return 0;
}

Summarize:

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

1.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();

	return 0;
}
  • The file input stream object can read data in binary mode through the read function

Guess you like

Origin blog.csdn.net/cui_yonghua/article/details/131426058