c++ fstream创建文件

// 实现文件的读取和写入

#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	string input_filename = "D:\\MVCVAE\\input.txt";
	ifstream input(input_filename);  // 定义一个ifstream并打开给定文件
	if (input.is_open()) {
		cout << " input.txt文件成功打开" << endl;
		input.close();
	}
	else
	{
		cout << " input.txt文未成功打开, 正在创建文件output.txt ...." << endl;
		string output_filename = "D:\\MVCVAE\\output.txt";
		ofstream output(output_filename, iostream::out);
		output.close();

	}
	return 0;
}

参考链接
https://blog.csdn.net/qq_38153833/article/details/122218460

// 实现文件的写入和读取

#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	string input_filename = "D:\\MVCVAE\\input.txt";
	string output_filename = "D:\\MVCVAE\\output.txt";
	string tmp_str; // 临时存的数据
	// 文件中存储的是 "rw_kkk   123456"

	ifstream input;  // 定义一个ifstream
	ofstream output; // 定义输出流

	input.open(input_filename); // 绑定文件,并打开
	if (input.bad()) //判定是否打的开
		cout << "文件没打开" << endl;

	output.open(output_filename, ios::app);
	
	// 遍历, 直到到了文件的尾部
	while (!input.eof())
	{
		input >> tmp_str;
		cout << "临时数据:" << tmp_str << endl;

		/* 当一个输入流被关联到一个输出流时, 任何试图从输入流读取数据
		的操作都会先刷新关联的输出流 */;
		output << tmp_str  << " " << endl;  

	}
	input.close();
	output.close();
	return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值