C + + learning notes - file operation

Posted by alireza on Sun, 02 Jan 2022 08:46:02 +0100

1. File operation

The data generated when the program runs belongs to temporary data. Once the program runs, it will be released. The data can be persisted through the file. The file operation in C + + needs to include the header file < fsstream >

1.1 document type classification

(1) Text file: the file is stored in the computer in the form of ASCII code of text;

(2) Binary files: files are stored in the computer in binary form of text, and users generally can't read them directly

1.2 three categories of operation documents:

(1) ofstream: write operation; (2) i fstream: read operation; (3) fsstream: read and write operation

1.3 text file - write file steps

(1) Include header file -- -#include < fsstream >

(2) Create a stream object -- ofstream ofs;

(3) Open file -- ofs.open("file path", opening method);

(4) Write data -- ofs < < "written data";

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

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

For example, write the file ios::binary|ios::out in binary mode

//----------------Exercise 31: file operations------------------------------------
void test()//Write file
{
	//1. Include header file < fstream >
	//2. Create flow object
	ofstream ofs;
	//3. Open file
	ofs.open("test.text",ios::out);
	//4. Write data
	ofs<<"Name: Zhang San"<<endl;
	ofs<<"Age: 20"<<endl;
	//5. Close file
	ofs.close();
}
int main()
{
	test();
	return 0;
}

1.4 text file - steps for reading files

(1) Include header file -- -#include < fsstream >

(2) Create a stream object -- ifstream ifs;

(3) Open the file and judge whether the file is successfully opened -- - ifs.open("file path", opening method)

(4) Read data - four ways to read

(5) Close the file -- ifs.close();

//Text file --- read file
void test()
{
	//1. Include header file -- -#include < fsstream >
	//2. Create flow object
	ifstream ifs;
	//3. Open the file and judge whether the file is opened successfully
	ifs.open("test.text",ios::in);
	if(!ifs.is_open())
	{
		cout<<"File open failed"<<endl;
		return;
	}
	//4. Read data - four ways to read
	//First kind
	char buf[1024]={0};
	while(ifs>>buf){
		cout<<buf<<endl;
	}
	//Second
	char buf[1024]={0};
	while(ifs.getline(buf,sizeof(buf))){
		cout<<buf<<endl;
	}
	//Third
	string buf;
	while(getline(ifs,buf))//Here, getline is a global function{
		cout<<buf<<endl;
	}
	//The fourth - not recommended, a character by character degree
	char c;
	while((c=ifs.get())!=EOF)//EOF end of file{
		cout<<c;
	}
	//5. Close file
	ifs.close();
}
int main()
{
	test();
	return 0;
}

Summary: (1) i fstream or fsstream class can be used to read files;

(2) Use the is_open function to judge whether the file is opened successfully; (3) close close the file;

1.5 binary file --- write file

(1) Read and write files in binary mode. The opening mode should be specified as ios::binary

(2) Binary file writing mainly uses the stream object to call the member function write

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

(4) Parameter explanation: the character pointer buffer points to a storage space in the memory, and len is the number of bytes read and written;

//-----------Binary file - write file-----------------------------
class Person
{
public:
	char m_Name[64];//full name
	int m_Age;//Age
};
void test01()
{
	//1. Include header file
	//2. Create flow object
	ofstream ofs;
	//ofstream ofs("person.txt",ios::out | ios::binary);// Constructor of class object
	//3. Open file
	ofs.open("person.txt",ios::out | ios::binary);
	//4. Write data
	Person p={"Zhang San",18};
	ofs.write((const char*)&p,sizeof(Person));
	//5. Close file
	ofs.close();
}
int main()
{
	test01();
	return 0;
}

1.6 binary file reading

Binary file reading mainly uses the stream object to call the member function read

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

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

//-------------Binary file -- read file-----------------------
class Person
{
public:
	char m_Name[64];
	int m_Age;
};
void test01()
{
	//1. Include header file
	//2. Create flow object
	ifstream ifs;
	//3. Open file --- judge whether the file is opened successfully
	ifs.open("person.txt",ios::in | ios::binary);
	if(!ifs.is_open())
	{
		cout<<"File open failed"<<endl;
		return;
	}
	//4. Read data
	Person p;
	ifs.read((char*)&p,sizeof(Person));
	cout<<"full name:"<<p.m_Name<<"Age:"<<p.m_Age<<endl;
	//5. Close file
	ifs.close();
}
int main()
{
	test01();
	return 0;
}