Skip to content

L4_07 文件操作

一、文件重定向

1.1 概念

将程序的输入输出重定向到文件,而不是标准输入输出设备。

1.2 输入重定向

cpp
#include <iostream>
using namespace std;

int main() {
    int num;
    cin >> num;  // 从文件读取而不是键盘
    cout << "读取的数字: " << num << endl;
    return 0;
}

运行方式:

program.exe < input.txt

1.3 输出重定向

cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Hello World!" << endl;  // 输出到文件而不是屏幕
    return 0;
}

运行方式:

program.exe > output.txt

1.4 同时重定向

program.exe < input.txt > output.txt

二、读操作

2.1 使用 ifstream

cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream inFile("input.txt");
    
    if (!inFile) {
        cout << "无法打开文件" << endl;
        return 1;
    }
    
    int num;
    inFile >> num;
    cout << "读取的数字: " << num << endl;
    
    inFile.close();
    return 0;
}

2.2 读取字符串

cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream inFile("input.txt");
    string line;
    
    while (getline(inFile, line)) {
        cout << line << endl;
    }
    
    inFile.close();
    return 0;
}

2.3 二进制读取

cpp
#include <fstream>
using namespace std;

int main() {
    ifstream inFile("data.bin", ios::binary);
    
    int arr[5];
    inFile.read((char*)arr, sizeof(arr));
    
    inFile.close();
    return 0;
}

三、写操作

3.1 使用 ofstream

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

int main() {
    ofstream outFile("output.txt");
    
    if (!outFile) {
        cout << "无法创建文件" << endl;
        return 1;
    }
    
    outFile << "Hello World!" << endl;
    outFile << 42 << endl;
    
    outFile.close();
    return 0;
}

3.2 追加写入

cpp
#include <fstream>
using namespace std;

int main() {
    ofstream outFile("output.txt", ios::app);
    
    outFile << "追加的内容" << endl;
    
    outFile.close();
    return 0;
}

3.3 二进制写入

cpp
#include <fstream>
using namespace std;

int main() {
    ofstream outFile("data.bin", ios::binary);
    
    int arr[] = {1, 2, 3, 4, 5};
    outFile.write((char*)arr, sizeof(arr));
    
    outFile.close();
    return 0;
}

四、读写操作

4.1 同时读写文件

cpp
#include <fstream>
using namespace std;

int main() {
    fstream file("data.txt", ios::in | ios::out | ios::trunc);
    
    if (!file) {
        cout << "无法打开文件" << endl;
        return 1;
    }
    
    // 写入
    file << "Hello World!" << endl;
    
    // 移动到文件开头
    file.seekg(0);
    
    // 读取
    string line;
    getline(file, line);
    cout << "读取内容: " << line << endl;
    
    file.close();
    return 0;
}

4.2 文件指针操作

cpp
#include <fstream>
using namespace std;

int main() {
    fstream file("data.txt", ios::in | ios::out);
    
    // 获取当前位置
    streampos pos = file.tellg();
    cout << "当前位置: " << pos << endl;
    
    // 移动到指定位置
    file.seekg(5);  // 移动到第5个字节
    
    file.close();
    return 0;
}

五、示例程序

5.1 文件复制

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

int main() {
    ifstream source("source.txt", ios::binary);
    ofstream dest("dest.txt", ios::binary);
    
    if (!source || !dest) {
        cout << "文件打开失败" << endl;
        return 1;
    }
    
    char buffer[1024];
    while (source.read(buffer, sizeof(buffer))) {
        dest.write(buffer, source.gcount());
    }
    
    source.close();
    dest.close();
    
    cout << "文件复制完成" << endl;
    return 0;
}

5.2 文件统计

cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream file("input.txt");
    string word;
    int count = 0;
    
    while (file >> word) {
        count++;
    }
    
    cout << "单词数量: " << count << endl;
    
    file.close();
    return 0;
}

百炼成钢,融会贯通