类别:
头文件
#include
using namespace std;
PS:文件位置的写法有两种,一种是"D:/VSCode/ConsoleApplication1/sunshine.txt"
另一种是"D:\\VSCode\\ConsoleApplication1\\sunshine.txt"
。
详解见:https://blog.csdn.net/sandalphon4869/article/details/86767978
string path = "D:\\VSCode\\ConsoleApplication1\\sunshine.txt";
ofstream outfile(path);
//ofstream outfile("D:\\VSCode\\ConsoleApplication1\\sunshine.txt");
ofstream outfile;
outfile.open("D:\\VSCode\\ConsoleApplication1\\sunshine.txt");
mode | 意义 |
---|---|
ios::in | 为输入(读)而打开文件(ifstream默认) |
ios::out | 为输出(写)而打开文件(ofstream默认) |
ios::ate | 初始位置:文件尾(at end) |
ios::app | 所有输出附加在文件末尾(append) |
ios::trunc | 如果文件已存在则先删除该文件(truncate) |
ios::_Nocreate | 打开一个文件时,如果文件不存在,不创建文件(对ofstream生效) |
ios::_Noreplace | 打开一个文件时,如果文件不存在,创建该文件(ofstream默认属性) |
使用:
string path = "D:\\VSCode\\ConsoleApplication1\\sunshine.txt";
ofstream outfile(path,ios::ate);
//ofstream outfile("D:\\VSCode\\ConsoleApplication1\\sunshine.txt");
ofstream outfile;
outfile.open("D:\\VSCode\\ConsoleApplication1\\sunshine.txt",ios::trunc);
is_open()
成功打开返回true,失败返回false。
例子:
ifstream infile(inpath);
if (!infile.is_open())
{
cout << "读取失败\n";
}
bad()
如果在读写过程中出错,返回 true 。例如:当我们要对一个不是打开为写状态的文件进行写入时,或者我们要写入的设备没有剩余空间的时候。
fail()
除了与bad() 同样的情况下会返回 true 以外,加上格式错误时也返回true ,例如当想要读入一个整数,而获得了一个字母的时候。
eof()
如果读文件到达文件末尾,返回true。
good()
这是最通用的:如果调用以上任何一个函数返回true 的话,此函数返回 false 。
(因为上述三种状态文件都不能进行正常的读写操作,出错,出错,结束。)
调用函数close(),可以关闭流对象所指向的文件
outfile.close();
infile.close();
PS:getline()在
头文件中:https://blog.csdn.net/sandalphon4869/article/details/99455492
#include
#include
using namespace std;
int main()
{
string path="D:\\VSCode\\ConsoleApplication1\\sunshine.txt";
ofstream outfile(path);
if (!outfile.is_open())
{
cout << "写入失败\n";
}
outfile << "hello\n";
outfile.close();
return 0;
}
#include
#include
#include
using namespace std;
int main()
{
string path="D:\\VSCode\\ConsoleApplication1\\sunshine.txt";
ifstream infile(path);
if (!infile.is_open())
{
cout << "读取失败\n";
}
string buff;
while (!infile.eof())
{
getline(infile,buff);
cout << buff << endl;
}
infile.close();
return 0;
}
//默认不创建
#include
#include
#include
using namespace std;
int main()
{
string path = "D:\\VSCode\\ConsoleApplication1\\nonexistent.txt";
ifstream infile(path);
if (!infile.is_open())
{
cout << "读取失败\n";
infile.close();
return 0;
}
infile.close();
return 0;
}
//_Noreplace不创建
#include
#include
#include
using namespace std;
int main()
{
string path = "D:\\VSCode\\ConsoleApplication1\\nonexistent.txt";
ifstream infile(path,ios::_Noreplace);
if (!infile.is_open())
{
cout << "读取失败\n";
infile.close();
return 0;
}
infile.close();
return 0;
}
默认创建
#include
#include
using namespace std;
int main()
{
string path = "D:\\VSCode\\ConsoleApplication1\\nonexistent.txt";
ofstream outfile(path);
if (!outfile.is_open())
{
cout << "写入失败\n";
outfile.close();
return 0;
}
outfile << "hello\n";
outfile.close();
cout << "创建并写入成功\n";
return 0;
}
ios::_Noreplace创建
#include
#include
using namespace std;
int main()
{
string path = "D:\\VSCode\\ConsoleApplication1\\nonexistent.txt";
ofstream outfile(path,ios::_Noreplace);
if (!outfile.is_open())
{
cout << "写入失败\n";
outfile.close();
return 0;
}
outfile << "hello\n";
outfile.close();
cout << "创建并写入成功\n";
return 0;
}
ios::_Nocreate可以不创建
#include
#include
using namespace std;
int main()
{
string path = "D:\\VSCode\\ConsoleApplication1\\nonexistent.txt";
ofstream outfile(path,ios::_Nocreate);
if (!outfile.is_open())
{
cout << "写入失败\n";
outfile.close();
return 0;
}
outfile << "hello\n";
outfile.close();
cout << "创建并写入成功\n";
return 0;
}
参考资料:
https://blog.csdn.net/qq_34097715/article/details/79970860
https://blog.csdn.net/Hunter_pcx/article/details/78563197#commentBox
https://www.cnblogs.com/zhuzhenwei918/p/8565478.html