在C语言中,如何到达指定目录?

发布于 2024-09-29 19:24:41 字数 360 浏览 1 评论 0原文

我必须编写一个程序,需要在指定目录中索引文件。我已经完成了索引部分,但我遇到的问题是如何导航到目录。 例如,当我启动程序时,它会询问“您想要索引哪个目录”,然后输入是“usr/Documents/CS/Assignment4”,我如何进入“Assignment4”目录?我知道需要递归,但我对目录在 C 中如何工作感到非常困惑。假设我的源文件位于“usr/Documents/SourceCode”中,那么我应该怎么做才能到达Assignment4?

我知道我听起来好像我想要所有的答案,但我完全不知道目录是如何工作的,而且我的书很糟糕。因此,即使您拥有的只是一个关于这方面的优秀教程的链接,那也太棒了。

我正在运行Linux,确切地说是Ubuntu。 GCC 是编译器。

I have to do a program where I need to index the files in a specified directory. I've gotten the indexing part down, but what I'm having trouble with is how to navigate to the directory.
For example, say when I start the program, it will ask "What directory would you like to index," And then the input would be "usr/Documents/CS/Assignment4," how do I get to the "Assignment4" directory? I know recursion is needed, but I'm really confused as to how directories work in C. Say my source file is in "usr/Documents/SourceCode," then what should I do to get to Assignment4?

I know I sound like I want all the answers, but I'm completely lost as to how directories work, and the book I have sucks. So even if all you have is a link to a good tutorial on this, that would be fantastic.

I'm running Linux, Ubuntu to be exact. GCC is the compiler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断桥再见 2024-10-06 19:24:41

C 编程语言没有文件系统的概念。这是一个操作系统特定的问题。

根据你问题中的目录风格,虽然听起来你在 unix / linux 风格的系统上。如果是这种情况,那么您正在寻找 opendir 函数

The C programming language doesn't have a notion of a file system. This is instead an operating system specific question.

Based on the style of directory in your question though it sounds like you're on a unix / linux style system. If that's the case then you're looking for the opendir function

九命猫 2024-10-06 19:24:41

在 C 中递归遍历目录的过程如下:

使用 opendirreaddir 列出目录条目。我可能不应该这样做,但我发布了完整的代码示例(无错误处理),因为您必须做很多小事情才能确保正确使用 API:

DIR           *dir;
struct dirent *de;
const char    *name;

dir = opendir(dirpath);
if (dir == NULL) {
    /* handle error */
}

for (;;) {
    errno = 0;
    de = readdir(dir);
    if (de == NULL) {
        if (errno != 0) {
            /* handle error */
        } else {
            /* no more entries left */
            break;
        }
    }

    /* name of file (prefix it with dirpath to get a usable file path) */
    name = de->d_name;

    /* ignore . and .. */
    if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
        continue;

    /* do something with the file */
}

if (closedir(dir) != 0) {
    /* handle error */
}

在处理每个文件时,请务必在其前面添加 dirpath(如果需要,还可以加上斜线)。您还可以使用 chdir 来下降和上升,但它在实践中引入了复杂性(例如,您不能同时遍历两个目录),因此我个人建议保持当前工作目录固定并使用字符串操作来连接路径。

要确定路径是否是目录(以及是否应该 opendir() ),我建议使用 lstat() 而不是 stat( ),因为后者遵循符号链接,这意味着您的目录遍历可能会陷入循环,您最终会得到一些 像这样的 ctags 输出

当然,由于目录结构本质上是递归的,因此递归在遍历过程中发挥了自然的作用:当子路径是目录时进行递归调用。

Recursively traversing a directory in C goes something like this:

Use opendir and readdir to list the directory entries. I probably shouldn't be doing this, but I'm posting a full code sample (sans error handling) because there are a bunch of little things you have to do to ensure you're using the API correctly:

DIR           *dir;
struct dirent *de;
const char    *name;

dir = opendir(dirpath);
if (dir == NULL) {
    /* handle error */
}

for (;;) {
    errno = 0;
    de = readdir(dir);
    if (de == NULL) {
        if (errno != 0) {
            /* handle error */
        } else {
            /* no more entries left */
            break;
        }
    }

    /* name of file (prefix it with dirpath to get a usable file path) */
    name = de->d_name;

    /* ignore . and .. */
    if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
        continue;

    /* do something with the file */
}

if (closedir(dir) != 0) {
    /* handle error */
}

When working with each file, be sure to prepend the dirpath to it (along with a slash, if needed). You could also use chdir to descend and ascend, but it introduces complications in practice (e.g. you can't traverse two directories simultaneously), so I personally recommend keeping your current working directory stationary and using string manipulation to concatenate paths.

To find out if a path is a directory or not (and hence whether you should opendir() it), I recommend using lstat() rather than stat(), as the latter follows symbolic links, meaning your directory traversal could get caught in a loop and you'll end up with something like this ctags output.

Of course, since directory structure is recursive in nature, recursion plays a natural role in the traversal process: make a recursive call when a child path is a directory.

怎樣才叫好 2024-10-06 19:24:41

目录的名称只是一个字符串。

所以 opendir("文件名");将使读取目录“文件”成为可能。

然而,您也许应该开始考虑文件名和路径。

“usr/Documents/SourceCode”+“/../CS/Assignment4”与“usr/Documents/CS/Assignment4”相同,但是我假设您缺少前导“/”。

好吧,我不明白你怎么会迷失目录的工作原理。目录与 Windows 或 Mac OS X 中的“文件夹”没有什么不同。底线是硬盘有一个文件系统,而文件系统仅由“包含”文件(以及命名套接字等特殊文件)的文件夹/目录组成。 ,您现在应该对此不感兴趣)。

希望这至少有一点帮助。

安吉洛

The name of the directory is only a string.

So opendir("filename"); will make it possible to read the directory "file".

However you should perhaps start thinking in filenames and pathes.

"usr/Documents/SourceCode" + "/../CS/Assignment4" is the same as "usr/Documents/CS/Assignment4" however I assume you are missing the leading "/".

Well, I don't get how you can be lost how directories work. A directory is nothing different than a "folder" in Windows or in Mac OS X. Bottom line a hard disk has a filesystem and a filesystem only consists out of folders/directories that "contain" files (and special files like named sockets etc., this should not interest you right now).

Hope this helped at least a bit.

Angelo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文