如何在文件目录中读取

时间:2013-02-03 20:58:39

标签: c++

我正在用C ++编写一个程序,它将处理大量(数千)PPM图像,这些图像都存储在同一目录中。但是,我首先需要读取像素值。它们是标准命名空间中的内置函数还是我可以导入的库,它允许我一次读取所有文件而不假设我已经使用某种循环结构知道文件名?万一它有所作为我在Mac上编写程序。

1 个答案:

答案 0 :(得分:1)

为了避免包含boost::filesystem和所需的依赖项,我最终实现了这个功能:

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

//include headers required for directory traversal
#if defined(_WIN32)
    //disable useless stuff before adding windows.h
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
#else
    #include "dirent.h"
#endif


/**
Traverses the provided directory (non-recursively) and extracts the absolute paths to all the files in it.

Doesn't support non-ASCII file names.

@param directory the absolute path to the directory
@return a vector of file names (including extension)
*/
std::vector<std::string> Filesystem::GetFilesInDirectory(const std::string &directory)
{
    std::vector<std::string> output;

#if defined(_WIN32)

    //select all files
    std::string tempDirectory = directory + "*";

    //initialize the WIN32_FIND_DATA structure
    WIN32_FIND_DATA directoryHandle = {0};

    //set the directory
    std::wstring wideString = std::wstring(tempDirectory.begin(), tempDirectory.end());
    LPCWSTR directoryPath = wideString.c_str();

    //iterate over all files
    HANDLE handle = FindFirstFile(directoryPath, &directoryHandle);
    while(INVALID_HANDLE_VALUE != handle)
    {
        //skip non-files
        if (!(directoryHandle.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
        {
            //convert from WCHAR to std::string
            size_t size = wcslen(directoryHandle.cFileName);
            std::vector<char> buffer;
            buffer.resize(2 * size + 2);
            size_t convertedCharacters = 0;
            wcstombs_s(&convertedCharacters, buffer.data(), 2 * size + 2, directoryHandle.cFileName, _TRUNCATE);
            //trim the null characters (ASCII characters won't fill the vector, since they require fewer bytes)
            //convertedCharacters includes the null character, which we want to discard
            std::string file(buffer.begin(), buffer.begin() + convertedCharacters - 1);

            //add the absolute file path
            output.emplace_back(file);
        }

        if(false == FindNextFile(handle, &directoryHandle)) break;
    }

    //close the handle
    FindClose(handle);

#else

    DIR *directoryHandle = opendir(directory.c_str());
    if (NULL != directoryHandle)
    {
        dirent *entry = readdir(directoryHandle);
        while (NULL != entry)
        {
            //skip directories and select only files (hopefully)
            //if ((DT_DIR != entry->d_type) && (DT_UNKNOWN == entry->d_type))
            if (DT_REG == entry->d_type)
            {
                output.emplace_back(entry->d_name);
            }

            //go to next entry
            entry = readdir(directoryHandle);
        }
        closedir(directoryHandle);
    }

#endif

    return output;
}

我对上述不灵活/无用/平台相关的代码并不感到自豪,如果可能的话,我肯定会选择BOOST。顺便说一句,它没有在MAC上进行测试,所以请告诉我它是否有效。