IOS
istream (输入) ostream(输出)
ifstream (文件输入) iostream(输入输出) ofstream(文件输出)
fstream(文件输入输出)
一般用黄色标记的两个头文件
特别注意
相对路径是指.cpp的当前目录
还有一种是.exe文件的当前目录
open函数的写入和读取
ios::in 读文件
ios::out 写文件
ios::app 在后面追加文件
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
fstream file;
string str;
file.open(".\\1.txt", ios::out);
if (file.is_open())
{
cout << "打开文件成功" << endl;
}
else
{
cout << "打开文件失败" << endl;
}
file << "123" << endl;
file.close();
file.open(".\\1.txt", ios::in); //读取的时候用这个
//getline(file,str,'a') 这个意思是以字符a做分隔符,但不会读到a
//默认 getline(file,str) 这是以 \n(换行符) 做分割
while (getline(file,str))
{
cout << str.c_str() << endl;
}
file.close();
return 0;
}
来源:CSDN
作者:洛阳鱼紫怡
链接:https://blog.csdn.net/weixin_42148156/article/details/104115487