#include <iostream>
#include <dirent.h>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
bool ReadImages(const string& strPath) {
const std::string& directory_str = strPath;
DIR* directory = opendir(directory_str.c_str());
if (NULL == directory) {
cout << "opendir fail : " << directory_str << ", errno: " << errno;
return false;
}
int count = 0;
struct dirent* dir_result = NULL;
struct dirent directory_entry;
while (readdir_r(directory, &directory_entry, &dir_result) == 0 && dir_result != NULL) {
if (0 == std::strcmp(".", directory_entry.d_name)
|| 0 == std::strcmp("..", directory_entry.d_name)) {
continue;
}
std::string image_name = '/' == directory_str.back()
? directory_str + directory_entry.d_name
: directory_str + "/" + directory_entry.d_name;
count++;
cout<<"fileName = " << image_name <<endl;
}
closedir(directory);
return true;
}
int main(){
std::cout << "Hello, World!" << std::endl;
ReadImages(string("./"));
return 0;
}
Hello, World!
fileName = ./CMakeFiles
fileName = ./untitled
fileName = ./cmake_install.cmake
fileName = ./untitled.cbp
fileName = ./Makefile
fileName = ./CMakeCache.txt
bool createDirectory(const char* filename) {
bool bRet = true;
char *tag;
for (tag = (char*)filename; *tag; tag++) {
if (*tag == '/') {
char buf[256], path[256];
strcpy(buf, filename);
buf[strlen(filename) - strlen(tag) + 1] = 0;
strcpy(path, buf);
if (access(path, 6) == -1) {
mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
}
}
return bRet;
}