1. fstream 继承自iostream --> 要包含头文件#include<fstream>

2. 建立文件流对象

3. 打开文件夹

4. 测试是否打开成功

5. 进行读写操作

6. 关闭文件

#include<iostream>
#include<fstream>

using namespace std;

int main(){
    ifstream ifile;
    ofstream ofile;
    
    ifile.open("d:\\fileIn.txt");
    ofile.open("d:\\fileOut.txt");

    if (ifile.fail() || ofile.fail()){
        cerr << "open file fail\n";
        return EXIT_FAILURE;
    }

    char ch;
    ch = ifile.get();
    cout << ch << endl;
    while (!ifile.eof()){
        ofile.put(ch);
        ch = ifile.get();
    }

    ifile.close();
    ofile.close();

    int i;
    cin >> i;
    return 0;
}
View Code

相关文章: