C++ study notes seven-use ofstream ifstream to read and write txt files simply

The input and output of C++ are provided by the standard library. The standard library defines a family of types that support reading and writing (IO) of files and other devices that control windows. Some other types are also defined so that the string object can be operated like a file, so that we can convert between characters without IO.

istream //输入流类型,提供输入操作
ifstream //由istream派生而来,提供读文件功能
ostream //输出流类型,提供输出操作
ofstream //由ostream 派生而来,提供写文件功能
>>  //用于从istream对象中读入输入
<<  //用于把输出写到ostream

The IO type is defined in three separate header files:
iostream defines the read-write control window type
fstream defines reads and writes the named file type
sstream The defined type is used to read and write string objects stored in memory

Use the following examples to illustrate the usage, the following examples all include header files

include <fstream>

The following routine simply creates and reads and writes files

# include <iostream>
# include<string>
# include<vector>
# include <fstream>
using namespace std;

int main()
{
    
    
	string c;//存文档的字符串
	ofstream out("D:\\转换\\1.txt");// 使用一个默认初始化的方式,等价于 ofstream out; out.open("D:\\转换\\1.txt");
	//其中打开文件的方式在ios类中定义,有以下的几种方式
	//ios::in 为输入而打开文件
	//ios::output 为输出而打开文件
	//ios::app 保留文件原有的内容
	//ios::tunc 如果文件已存在则先删除该文件
	//以上方式能够通过(“|”)使用
	out << "this is a output file test" << endl;
	//值得注意的是如果文件夹中没有txt则会自己创建
	//如果想在文件中输入时不覆盖原有的内容,则需要ios::app
	ofstream output1("D:\\转换\\1.txt", ios::app);
	output1 << "is not cover" << endl;//将内容输出到文件内
	output1.close();
	//关闭
	vector<string> memory;
	ifstream read;//从文件中读取相关文件,并存储到vector中
	read.open("D:\\转换\\1.txt");
	while (read >> c)
	{
    
     
		memory.push_back(c);
	}
	for (vector<string> ::size_type flag = 0; flag != memory.size(); flag++)
	{
    
    
		cout << "the final result "<< memory[flag] << endl;
	}

	return 0;
}

The result runs as follows:

Insert picture description here

Insert picture description here

Program for more than a few points above summarize
1. << using >> operator, has been mentioned above, I personally have a simple understanding, "<<" for example the direction of the arrow on the left, "streaming "The direction of "is the direction of the arrow , so as to understand out << "this is a output file test" << endl; is to pass "this is a output file test" to the out file
2. In the process of reading the file, the txt When the content of the file is output to the character c, read.eof() is used,
eof() means that if it is read to the end of the file, it will return true. The members of other functions also have the following
bad()
if there is an error in the reading and writing process If it returns true,
fail()
will return true in the same situation as bad(), and it will also return true when there is a format error, for example, when you want to read an integer and get a letter.
good()
This is the most common: if calling any of the above functions returns true, this function returns false.

3.
When traversing the vector, size_type is used, which is mentioned in my previous article. In order to prevent crossing the border

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/108784611