写入文本文件
#include <iostream>
#include <fstream>//ofstream类需要包含的头文件
using namespace std;
void test01()
{
//1、包含头文件 fstream
//2、创建流对象
ofstream fout;
/*3、指定打开方式:
1.ios::out、ios::trunc 清除文件内容后打开
2.ios::app 文件末尾追加内容
*/
fout.open(R"(C:\Users\18757\Desktop\pythontext\out.txt)", ios::out);
//4、写内容
fout << "西施|19|极漂亮\n";
fout << "冰冰|21|漂亮\n";
fout << "幂幂|25|一般\n";
//5、关闭文件
fout.close();
}
int main()
{
test01();
}

读取文本文件
#include <iostream>
#include <fstream>//ifstream类需要包含的头文件
#include <string>
using namespace std;
void test01()
{
//1、包含头文件 fstream
//2、创建流对象
ifstream fin;
//3、打开方式只有:ios:in
fin.open(R"(C:\Users\18757\Desktop\pythontext\out.txt)", ios::in);
//4、一行行读取文件
string buffer;
/*方式一
while (getline(fin,buffer)) {
cout << buffer << endl;
}
*/
//方式二
while (fin >> buffer) {
cout << buffer << endl;
}
//5、关闭文件
fin.close();
}
int main()
{
test01();
}

getline补充
getline()的原型是istream& getline ( istream &is , string &str , char delim );
char delim表示遇到这个字符停止读入,通常系统默认该字符为’\n’,也可以自定义字符












![python算法和数据结构刷题[2]:链表、队列、栈](https://i-blog.csdnimg.cn/direct/34780a8f88ee40fdb0ceb39f2b996eb3.png)



![洛谷P4057 [Code+#1] 晨跑](https://i-blog.csdnimg.cn/direct/56e95a36685d45f3b473256e823d86c0.png)

