You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++中读取用户命名的多个文本文件并显示内容?

读取多个用户文本文件的C++实现

方法一:已知文件名列表时读取

如果已经明确要读取的文件名(比如sarah.txt、kevin.txt),可以直接逐个打开文件读取内容:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main() {
    // 要读取的文件名列表
    std::vector<std::string> filenames = {"sarah.txt", "kevin.txt"};

    for (const auto& filename : filenames) {
        std::ifstream input_file(filename);
        if (!input_file.is_open()) {
            std::cerr << "无法打开文件: " << filename << std::endl;
            continue;
        }

        std::string line;
        std::cout << "=== 文件 " << filename << " 内容 ===" << std::endl;
        // 逐行读取并输出
        while (std::getline(input_file, line)) {
            std::cout << line << std::endl;
        }
        input_file.close();
    }

    return 0;
}

方法二:遍历目录下所有用户文本文件

如果需要自动扫描目录下所有*.txt格式的文件(因为生成的文件都是用户名.txt),不同操作系统的实现略有差异:

Linux/macOS 平台

利用<dirent.h>头文件遍历目录:

#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>

bool is_txt_file(const std::string& filename) {
    size_t dot_pos = filename.find_last_of('.');
    return (dot_pos != std::string::npos) && (filename.substr(dot_pos) == ".txt");
}

int main() {
    const char* dir_path = "./"; // 当前目录,可替换为目标目录路径
    DIR* dir = opendir(dir_path);
    if (!dir) {
        std::cerr << "无法打开目录" << std::endl;
        return 1;
    }

    struct dirent* entry;
    while ((entry = readdir(dir)) != nullptr) {
        std::string filename = entry->d_name;
        // 跳过当前目录和上级目录,只处理txt文件
        if (filename == "." || filename == ".." || !is_txt_file(filename)) {
            continue;
        }

        std::ifstream input_file(filename);
        if (!input_file.is_open()) {
            std::cerr << "无法打开文件: " << filename << std::endl;
            continue;
        }

        std::string line;
        std::cout << "=== 文件 " << filename << " 内容 ===" << std::endl;
        while (std::getline(input_file, line)) {
            std::cout << line << std::endl;
        }
        input_file.close();
    }

    closedir(dir);
    return 0;
}

Windows 平台

利用Windows API的<windows.h>和<shlwapi.h>来遍历目录:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <shlwapi.h>

#pragma comment(lib, "shlwapi.lib")

int main() {
    const char* dir_path = ".\\*.txt"; // 当前目录下所有txt文件
    WIN32_FIND_DATAA find_data;
    HANDLE hFind = FindFirstFileA(dir_path, &find_data);

    if (hFind == INVALID_HANDLE_VALUE) {
        std::cerr << "未找到任何txt文件或无法打开目录" << std::endl;
        return 1;
    }

    do {
        // 跳过目录项
        if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            continue;
        }

        std::string filename = find_data.cFileName;
        std::ifstream input_file(filename);
        if (!input_file.is_open()) {
            std::cerr << "无法打开文件: " << filename << std::endl;
            continue;
        }

        std::string line;
        std::cout << "=== 文件 " << filename << " 内容 ===" << std::endl;
        while (std::getline(input_file, line)) {
            std::cout << line << std::endl;
        }
        input_file.close();
    } while (FindNextFileA(hFind, &find_data) != 0);

    FindClose(hFind);
    return 0;
}

内容的提问来源于stack exchange,提问作者penguinsoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 20:15:43