直接上代码,简单时效。
本质上就一句话:把ifstream 和ofstream 当成cin和cout来使用,只不过输入输出不是终端命令行,而是文件。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const char* const IN_PATH = "E:/my_learn/datasets/in.txt";
const char* const OUT_PATH = "E:/my_learn/datasets/out.txt";
int main()
{
//创建二维数组分别存储string数据
string str[25][2];
ifstream myfile(IN_PATH);
ofstream outfile(OUT_PATH, ios::trunc);
if (!myfile.is_open())
{
cout << "can not open this file" << endl;
return 0;
}
//从data1文件中读入string数据
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 2; j++)
{
myfile >> str[i][j];
}
}
//将数据输出至out.txt文件中
for (int i = 0; i < 25; i++)
{
outfile << str[i][0] << " " << str[i][1] << endl;
};
myfile.close();
outfile.close();
return 0;
}