C++之文件写入:ifstream、ofstream和fstream


一、C++的流读写文件

1.流

类别:

  • ifstream:从已有的文件读入(in,读入)
  • ofstream:向文件写内容(out,表示输出到文件,即写入)
  • fstream:打开文件供读写

头文件

#include<fstream>
using namespace std;

2.打开文件

PS:文件位置的写法有两种,一种是"D:/VSCode/ConsoleApplication1/sunshine.txt"另一种是"D:\\VSCode\\ConsoleApplication1\\sunshine.txt"
详解见:https://blog.csdn.net/sandalphon4869/article/details/86767978

(1)构造方式打开

string path = "D:\\VSCode\\ConsoleApplication1\\sunshine.txt";
ofstream outfile(path);
//ofstream outfile("D:\\VSCode\\ConsoleApplication1\\sunshine.txt");

(2)open()方法打开

ofstream outfile;
outfile.open("D:\\VSCode\\ConsoleApplication1\\sunshine.txt");

(3)指定打开方式mode

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);

(4)检测是否打开成功

is_open()

成功打开返回true,失败返回false。

例子:

ifstream infile(inpath);
if (!infile.is_open())
{
	cout << "读取失败\n";
}

3. 文件状态。

bad()

如果在读写过程中出错,返回 true 。例如:当我们要对一个不是打开为写状态的文件进行写入时,或者我们要写入的设备没有剩余空间的时候。

fail()

除了与bad() 同样的情况下会返回 true 以外,加上格式错误时也返回true ,例如当想要读入一个整数,而获得了一个字母的时候。

eof()

如果读文件到达文件末尾,返回true。

good()

这是最通用的:如果调用以上任何一个函数返回true 的话,此函数返回 false 。
(因为上述三种状态文件都不能进行正常的读写操作,出错,出错,结束。)

4.关闭文件

调用函数close(),可以关闭流对象所指向的文件

outfile.close();
infile.close();

二、例子

PS:getline()在<string>头文件中:https://blog.csdn.net/sandalphon4869/article/details/99455492

1.写入一个已存在的文件

#include <iostream>
#include<fstream> 
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;
}

2.读取一个已存在的文件

#include <iostream>
#include <fstream> 
#include <string>
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;
}

3.创建一个文件

(1)ifstream不能创建

//默认不创建
#include <iostream>
#include <fstream> 
#include <string>
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 <iostream>
#include <fstream> 
#include <string>
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;
}

(2)ofstream:默认创建,_Noreplace创建

默认创建

#include <iostream>
#include<fstream> 
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 <iostream>
#include<fstream> 
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 <iostream>
#include<fstream> 
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值