读取指定目录下的所有文件(windows 和 linux 版)

笔者这里用到了OpenCV,如果不需要用OpenCV代码的话,可以将这部分代码去掉即可。

windows  vs2015环境代码如下:

#include <io.h> // 结构体struct _finddata_t需要用到
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

char * fileLoadPath = "E:\\ubshare\\Cars\\102051724100";
char * fileDstPath = "E:\\ubshare\\Cars\\testdir\\";

void getFiles(string path, vector<string>& files)
{
    //文件句柄
    long   hFile = 0;
    //文件信息
    struct _finddata_t fileinfo;
    string p;
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            //如果是目录,迭代之
            //如果不是,加入列表
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
            }
            else
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

int main()
{
    vector<string> files;

    ////获取该路径下的所有文件
    getFiles(fileLoadPath, files);

    int size = files.size();
    for (int i = 0; i < size; i++)
    {
        string name = files[i].c_str() + strlen(fileLoadPath) + 1; // 从路径名中取出文件名
        cout << "files[i].c_str() = " << files[i].c_str() << ", name = " << name << endl;
        name.replace(name.find(".jpg"), 4, ".bmp"); // 将文件名的jpg后缀改为bmp后缀

        Mat img = imread(files[i].c_str());
        imshow(files[i].c_str(), img);
        string str = fileDstPath;
        str += name;
        imwrite(str, img); // 将图片按指定格式存入指定路径
    }
    waitKey(0);
    return 0;
}

linux版代码如下:

#include <iostream>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include <dirent.h>

using namespace std;
using namespace cv;

string gFileLoadPath = "/mnt/hgfs/ubshare/Cars/102051724100/group2/";
string gFileDstPath = "/mnt/hgfs/ubshare/Cars/102051724100result/";

int main()
{
    DIR *dir = opendir(gFileLoadPath.c_str());
    if (dir == NULL)
    {
        cout << "opendir error" << endl;
        return -1;
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL)
    {
        //if (entry->d_type == 4) continue; //It‘s dir
        cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;
        string name = entry->d_name;
        string imgdir = gFileLoadPath + name;
        Mat img = imread(imgdir.c_str());
        imshow(entry->d_name, img);

        string resultdir = gFileDstPath + name;
        imwrite(resultdir.c_str(), img);
    }
    closedir(dir);
    waitKey(0);
    //system("pause");
    return 0;
}
时间: 2024-08-09 02:18:43

读取指定目录下的所有文件(windows 和 linux 版)的相关文章

Java读取指定目录下的所有文件(子目录里的文件也可递归得到)

1 import java.io.File; 2 3 public class ReadFile { 4 5 public static void main(String[] args) { 6 7 // path是指定目录的绝对路径 8 String path = "/Users/tonychan/Workspaces/MyEclipse 2017 CI/Zhangjiajie/WebRoot/pics"; 9 getFile(path); 10 11 } 12 13 // 给定目录

Windows Phone获得IsolatedStorage中指定目录下的所有文件

在Windows Phone 中对隔离存储空间中的文件操作需要通过System.Io.IsolatedStorage下的类进行操作 获得指定文件夹下的所有文件: 参数:是指定文件夹的路径加上通配符,格式:\folder1\* List<string> GetFileNames(string _strFolder) { List<string> returnlst = new List<string>(); using (IsolatedStorageFile stora

iOS案例:读取指定目录下的文件列表

// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rights reserved. // /* *读取指定目录下的文件列表 */ #import <Foundation/Foundation.h> void myQuickMethod(); int main(int argc, const char * argv[]) { //文件操作对象 NSFil

Python--通过索引excel表将文件进行文件夹分类的脚本+读取指定目录下所有文件名的脚本

1.通过索引excel表将文件进行文件夹分类的脚本,此脚本由于将ip和id对应并生成对应id的文件夹将文件进行分类,也可以任意规定表格内容,通过vul_sc_ip.txt和xlsx文件进行索引. # -*- coding:utf8 -*- import sys import os import pandas as pd import shutil import stat def find(path,ip): # open the excel file df = pd.read_excel(pat

Java 读取指定目录下的文件名和目录名

需求:读取指定目录下的文件名和目录名 实现如下: package com.test.common.util; import java.io.File; public class ReadFile { /* * 读取指定路径下的文件名和目录名 */ public void getFileList() { File file = new File("D:\\"); File[] fileList = file.listFiles(); for (int i = 0; i < file

C#递归读取指定路径下的所有文件并保存至TreeView

1.代码如下: /// <summary> /// 递归读取指定路径下的所有文件信息 /// </summary> /// <param name="path"></param> /// <param name="node"></param> private void DIGuiGetFile(string path, TreeNode node) { if (!Directory.Exists

python之查询指定目录下的最新文件

使用os模块查询指定目录下的最新文件 1 import os 2 3 # 输入目录路径,输出最新文件完整路径 4 def find_new_file(dir): 5 '''查找目录下最新的文件''' 6 file_lists = os.listdir(dir) 7 file_lists.sort(key=lambda fn: os.path.getmtime(dir + "\\" + fn) 8 if not os.path.isdir(dir + "\\" + f

c# 获取指定目录下的所有文件并显示在网页上

参考文献: FileInfo 的使用  https://msdn.microsoft.com/zh-cn/library/system.io.fileinfo_methods(v=vs.110).aspx 网页表格的生成  http://www.w3school.com.cn/html/html_tables.asp C# 通过文件扩展名获取图标和描述 http://www.csframework.com/archive/2/arc-2-20110514-1478.htm   http://ww

Java API 读取HDFS目录下的所有文件

/** * 获取1号店生鲜食品的分类id字符串 * @param filePath * @return */ public String getYHDSXCategoryIdStr(String filePath) { final String DELIMITER = new String(new byte[]{1}); final String INNER_DELIMITER = ","; // 遍历目录下的所有文件 BufferedReader br = null; try { F