About the reading and writing operations of files in C/C++


Preface

Because the data generated in the heap area or stack area after running is recycled by the system after the program is executed and will not be saved, so if you need to save the data of this run, you can consider using file operations to store the data of this run. into a text file.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Steps to write files (5-step method)

1.1 Steps to write files

1.Introduction (introduction of header files)

2. Create (create stream object)

3. Open (the open function opens the file)

4. Write (write data to the open file)

5.Close (close the file)

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<fstream>//1.引入操作文件的头文件
using namespace std;
int main()
{
	//2.创建流对象
	ofstream fout;
	//3.打开文件
	fout.open("test01.txt",ios::out); //open打开文件,若在此路径下没有则创建一个文件
	//4.写数据
	fout << "Hello World!" << endl;
	fout << 123 << endl;           //类似于cout(此时endl也是用于换行)
	//5.关闭文件
	fout.close();
	cout << "写文件操作完成" << endl;
	system("pause");
	return 0;
}

1.2 Some additional notes

1.2.1 Several methods of creating objects

 1.2.2 Several opening methods

 As shown in the figure: If you use the app to open the visible running results, append to the end of the file last written.

Simultaneous opening methods can also be used together. like:

fout.open("test01.txt",ios::out | ios::binary); means writing the file and opening it in binary mode.

1.2.3 open function description

1. There are two parameters in the open function. The first is the address of the file to be opened, and the second is the opening method.

2. As shown in the figure, you can also use address mode to write files.

 3. The above figure uses escape characters to solve the address problem, or you can use the original literal method, as follows.

 It can be seen that the addition was successful.

2. Read files

1.1 Steps to read files

1.Introduction (introduction of header files)

2. Create (create stream object)

3. Open (the open function opens the file)

4. Read (read the data of the file)

5.Close (close the file)

The code is as follows (example):

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<fstream>//1.引入操作文件的头文件
using namespace std;
int main()
{
	//2.创建流对象
	ofstream fout;
	ifstream fin;
	//3.打开文件 
	fin.open(R"(D:\code文件\test1\文件操作\文件操作\test01.txt)",ios::in);
	if (fin.is_open() == false) //判断文件是否打开成功 打开成功返回ture 失败则返回false
	{
		cout << "打开文件失败!" << endl;
		//return 0;
	}
	//4.读数据
	string buffer;
	while (getline(fin,buffer)) //getline多个重载其中一个  getline 把文件的数据一行一行的读出来
	{                                       //放到buffer中当读取完毕返回空结束循环   
		cout << buffer << endl; //打印输出到控制台
	}
	//类似于cout(此时endl也是用于换行)
	//5.关闭文件
	fin.close();
	cout << "读文件操作完成" << endl;
	system("pause");
	return 0;
}

As shown in the figure, the reading is successful.


Summarize

This time I briefly introduce the basic operations of some files, I hope it can help everyone.

Guess you like

Origin blog.csdn.net/m0_74058637/article/details/131641962