简体   繁体   English

C ++ dirent.h和获取绝对路径的语法

[英]c++ dirent.h and syntax for getting absolute path

I am learning to use dirent.h. 我正在学习使用dirent.h。 While the process is fascinating and interesting, I have ran into a problem with using d_name. 尽管过程令人着迷且有趣,但是我在使用d_name时遇到了问题。

I want to do two things using d_name. 我想使用d_name做两件事。

  1. recursively search through the sub-directories. 递归搜索子目录。 To this, when I encounter DT_DIR type files, I will make recursive call to the function 为此,当我遇到DT_DIR类型的文件时,我将对该函数进行递归调用

     void iterateDirectory(char* filePath){ DIR* dirPtr; dirent* entry; entry = readdir(dirPtr); ... } 

    within itself using d_name of the directory as a new char* filePath parameter. 在内部使用目录的d_name作为新的char * filePath参数。 So, 所以,

     if(dirEntry->d_type == DT_DIR){ entry->d_name; iterateDirectory(entry->d_name); ... } 
  2. Open all files within a directory. 打开目录中的所有文件。 To do this, when I encounter DT_REG files, I will create ifstream object and open the file using d_name. 为此,遇到DT_REG文件时,将创建ifstream对象并使用d_name打开文件。 So, 所以,

     if(dirEntry->d_type == DT_REG){ entry->d_name; ifstream fin(entry->d_name); if(fin.is_open) cout<<"Opened"<<endl; else cout<<"Not Opened"<<endl; ... } 

The problem I am running into, is that neither the void iterateDirectory() function nor the ifstream fin() seems to recognize the entry->d_name as a valid input. 我遇到的问题是,无效的iterateDirectory()函数和ifstream fin()似乎都不能将entry-> d_name识别为有效输入。 When I call the iterate function using d_name or use ifstream with entry->d_name, my checks to see if the directory or file is open fails. 当我使用d_name调用迭代函数或将ifstream与entry-> d_name一起使用时,检查目录或文件是否打开的检查失败。 The function itself is working, as I've checked the exact same function with different char* inputs. 该函数本身正在运行,因为我已经用不同的char *输入检查了完全相同的函数。 The only problem I can think of is that my function is not taking in the absolute path as the parameter. 我能想到的唯一问题是我的函数没有采用绝对路径作为参数。

My questions is how can I find the absolute path of a given file or sub-directory at the point of iteration. 我的问题是,如何在迭代时找到给定文件或子目录的绝对路径。 My initial solution was to make use of "." 我最初的解决方案是使用“。” as that is the current directory. 因为那是当前目录。 Store the address of "." 存储“。”的地址。 into a string, and append "\\"+entry->d_name. 到字符串中,并附加“ \\” + entry-> d_name。 But I think the syntax is wrong. 但是我认为语法是错误的。

Am I right about the absolute path problem? 我对绝对路径问题是否正确? or is there another problem I am missing? 还是我还缺少另一个问题? If it is the absolute path problem, what is the syntax for getting the absolute path of a file? 如果这是绝对路径问题,那么获取文件绝对路径的语法是什么?

PS PS

I've been informed in the past to minimize the amount of code I upload onto stack overflow for questions, and I presented what I figure to be the smallest required code. 过去,我一直被告知要尽量减少上传到堆栈溢出问题的代码量,并介绍了我认为是最小的所需代码。 In case the information presented above is insufficient, I am linking github page for the code. 如果上面提供的信息不足,我将链接github页面以获取代码。

https://github.com/ForeverABoy/dirent.h_practice/blob/master/directoryIterator.cpp https://github.com/ForeverABoy/dirent.h_practice/blob/master/directoryIterator.cpp

Any and all helps are appreciated. 任何和所有帮助,不胜感激。 Thank you! 谢谢!

Thanks to Sam Varshavchik, I have figured out the problem. 感谢Sam Varshavchik,我解决了这个问题。

The problem indeed was in not calling the functions using full path names. 问题的确实是没有使用完整路径名调用函数。 I knew this and immediately tried fixing it with realpath(). 我知道这一点,并立即尝试使用realpath()对其进行修复。 The problem was that I was using realpath on the entry->d_name instead of directory name. 问题是我在entry-> d_name而不是目录名上使用了realpath。

realpath(entry->d_name, buffer);

This returned the build directory instead of the input directory. 这将返回构建目录而不是输入目录。 I assume it is because while running the code, the path from variable d_name would be indeed in the build directory. 我认为是因为在运行代码时,变量d_name的路径确实在构建目录中。

char* fullPath = realpath(inputPath, buffer);

This gives me the actual path I entered. 这给了我输入的实际路径。 From there, I just turned the path into string and appended the path as I encountered directories or files. 从那里,我只是将路径转换为字符串,并在遇到目录或文件时追加了路径。

Thanks again Sam. 再次感谢山姆。 You are right. 你是对的。 When I read and fully understood what realpath() manual was saying, it all made sense. 当我阅读并完全理解realpath()手册在说什么时,这一切都是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM