// 实现文件的读取和写入
#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;
}