C++ 如何循环读取一个文件夹下的所有文件并打印出内容(LINUX篇)?

由于<io.h>及调用里面的函数在linux下会报错,所以linux下用另外的方法来实现。

#include <iostream>
#include <fstream> 
#include <sys/types.h>
#include <dirent.h>
#include <vector>
#include <cstring>

using namespace std;

int main()

{
    vector<string> file_name;
    string path = "D:\\test\\";

    GetFileNames(path, file_name);

    return 0;
}

void GetFileNames(string path,vector<string>& filenames)
{
    DIR *pDir;
    struct dirent* ptr;
    if(!(pDir = opendir(path.c_str()))){
        cout<<"Folder doesn't Exist!"<<endl;
        return;
    }
    while((ptr = readdir(pDir))!=0) {
        if (strcmp(ptr->d_

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用C++的标准库和Linux系统的文件操作来实现这个功能。下面是一个示例代码: ```cpp #include <iostream> #include <fstream> #include <dirent.h> #include <cstring> // 读取图片的RGB信息并保存到txt文件 void processImage(const char* imagePath) { // 打开图片文件 std::ifstream imageFile(imagePath, std::ios::binary); if (!imageFile) { std::cerr << "Failed to open image: " << imagePath << std::endl; return; } // 获取图片文件名 std::string imageName = imagePath; size_t slashPos = imageName.find_last_of("/"); if (slashPos != std::string::npos) { imageName = imageName.substr(slashPos + 1); } // 构建对应的txt文件名 std::string txtPath = imageName + ".txt"; // 删除已存在的txt文件 std::remove(txtPath.c_str()); // 创建txt文件并打开 std::ofstream txtFile(txtPath); if (!txtFile) { std::cerr << "Failed to create txt file: " << txtPath << std::endl; return; } // 读取图片的RGB信息 unsigned char pixel[3]; while (imageFile.read(reinterpret_cast<char*>(pixel), sizeof(pixel))) { // 将RGB信息写入txt文件 txtFile << static_cast<int>(pixel[0]) << " " << static_cast<int>(pixel[1]) << " " << static_cast<int>(pixel[2]) << std::endl; } // 关闭文件 imageFile.close(); txtFile.close(); std::cout << "Saved RGB information to: " << txtPath << std::endl; } // 遍历文件夹并处理每张图片 void processImagesInFolder(const char* folderPath) { // 打开文件夹 DIR* dir = opendir(folderPath); if (!dir) { std::cerr << "Failed to open folder: " << folderPath << std::endl; return; } dirent* entry; // 遍历文件夹的每个文件 while ((entry = readdir(dir)) != nullptr) { if (entry->d_type == DT_REG) { // 构建文件的完整路径 std::string filePath = folderPath; filePath += "/"; filePath += entry->d_name; // 处理图片文件 processImage(filePath.c_str()); } } // 关闭文件夹 closedir(dir); } int main() { const char* folderPath = "/path/to/your/folder"; // 替换为你的文件夹路径 processImagesInFolder(folderPath); return 0; } ``` 注意替换代码的`/path/to/your/folder`为您的文件夹路径。此代码将遍历指定文件夹的所有图像文件,对每个图像文件读取RGB信息并将其保存到同名的txt文件。如果txt文件已经存在,将会在保存之前删除它。 请确保您的编译环境支持C++11标准或更高版本,并将代码保存为`.cpp`文件,然后使用`g++`编译器进行编译,例如: ``` g++ main.cpp -o image_processing ``` 然后运行生成的可执行文件: ``` ./image_processing ``` 请注意,此代码没有使用OpenCV库,仅使用了C++的标准库和Linux系统的文件操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值