c++进行读取和保存

使用fstreamsstream

#include<iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main() {
//------------读取文件----------
    //定义文件对象
    ifstream inFile;
    ofstream outFile;
    string line;
    int arr[3];
    inFile.open("decode.in", ios::in);

    if (!inFile.is_open())
    {
        cout << "读取文件失败" << endl;
        return 0;
    }else{
        //getline可以文件文件内容保存每行的数据到line
        while (getline(inFile,line))
        {
            //使用stringstream
            stringstream ss(line);
            //读取当前行每列的内容,并保存到数组
            int Column=0;
            while (ss >> arr[Column]) {
                Column++;
            }
        }
    }
// --------保存文件----------
    ofstream outFile;

    //打开文件,ios::app是追加数据
    outFile.open("decode.out",ios::app);

    //存放数据
    outFile <<"A"<<" "<<"B"<< "\n";
    outFile.close();

    //清空decode.out文件的内容,ios::trunc
    outFile.open("decode.out",ios::trunc);
    outFile.close();
    return 0;

}