C++ write and read operations on txt files

1. File flow knowledge

insert image description here

Excerpted from C++ Chinese Network

  • ifstream is the input file stream (that is, the content in the file is obtained through the object it defines)
  • ofstream is the output file stream (writes content to file)
    Notice: To use the input and output file stream to include the header file#include<fstream>

2. Write the file

  • First define an input object with ofstreamofstream outf;

The outf here can be customized

  • Then outf.open(文件路径)open the file with

Notice: (1) If the path here is not written, the file will be automatically stored in the directory where the project is located.
(2) The / in the path of the file conflicts with the escape character in C++ , so it needs to be changed to double slashes

  • Write outf<<"666";"666" to the file (for example)
  • outf.close()Close the file
    and watch the code demo below :
int main()     //注意#include<fstream>头文件
{
    
    
	ofstream outf;    //用ofstream类定义输入对象
	outf.open("D://test01.txt");  //文件绝对路径(注意双斜杠取消转义)
	outf<<"I am DB"<<endl;    
	outf<<"I like learning"<<endl;   //写入内容
	outf.close();
}

The result display:
There is a test01.txt file under the D drive
expand:
(1) Different opening methods can be added when the file is opened (for example, if you want to continue writing on the original content when opening the file , you must add ios::app, if you do not add the default opening method, the original content of the file will be overwritten )
insert image description here

Excerpted from c language Chinese website

  • Use of ios::app
int main()
{
    
    
	string s;
	ofstream outf;    //用ofstream类定义输入对象
	outf.open("D://test01.txt");  //文件绝对路径(注意双斜杠取消转义)
	outf<<"I am DB"<<endl;    
	outf<<"I like learning"<<endl;   //写入内容
	outf.close();

    ifstream inf;     //用ifstream类定义输入对象
	outf.open("D://test01.txt",ios::app);  //再次用ios::app的方式打开txt文件
	outf<<"这是一行追加内容!"<<endl;
	inf.open("D://test01.txt");
	while(getline(inf,s)) //获取一行内容
	{
    
    
		cout<<s<<endl;
	}
	inf.close();
	outf.close();
}

result :
insert image description here

3. Output of file content

The content and file writing are similar
to see the code example :
1.Output file content line by line

int main() 
{
    
    
	string s;
	ofstream outf;    //用ofstream类定义输入对象
	outf.open("D://test01.txt");  //文件绝对路径(注意双斜杠取消转义)
	outf<<"I am DB"<<endl;    
	outf<<"I like learning"<<endl;   //写入内容
	
	ifstream inf;     //用ifstream类定义输入对象
	inf.open("d://test01.txt");  //注意输出流要先打开文件!!!
	while(getline(inf,s)) //获取一行内容
	{
    
    
		cout<<s<<endl;
	}
	inf.close();
	outf.close();
}

result :
insert image description here

Getline usage reference: c language Chinese website

2 .Output one by one separated by spaces and newlines

int main()
{
    
    
   string filename="D://test03.txt";
   ofstream fout;
   fout.open(filename);   //可以用字符串赋值为文件路径
   fout<<"This is the second time"<<endl;
   fout<<"I will be the first"<<endl;
   //以下为文件内容输出部分
   ifstream fin;
   fin.open(filename);  //注意读取文件时,要先打开文件
   char buf[1024]={
    
    0};  //创建一个数组用于临时存放从文件获取的内容
   while(fin>>buf)      
   	cout<<buf<<endl;   //在while循环中逐个输出
   fout.close();
   fin.close();
   system("pause");
   return 0;
}

Result :
insert image description here
More operations on txt files will be updated later! ! !
Thanks for the support! ! !

Guess you like

Origin blog.csdn.net/weixin_74334323/article/details/130674968