0

我假设有两个文本文件abc.txtdef.txt文件夹“我的”。我有一个程序直接进入该文件夹并搜索特定文件,如果该特定文件找到然后如何访问该文件的信息。

我知道如何通过文件处理在 C 中读取写入文件,但我不知道如何搜索特定文件,然后读取该特定文件以匹配文件中的特定字符串。

**All these things access through file handling in C.**

因此,如果有人有任何解决方案,我将不胜感激

示例将是最好的理解方式。

提前致谢

4

1 回答 1

1

要在 Linux 中获取目录中的文件列表,您可以使用 'dirent.h' 中的 'opendir'、'readdir' 和 'closedir' 函数。例如:

#include <dirent.h>
#include <stdio.h>

int ListDir(const char *pDirName)
{
    DIR *pDir;
    struct dirent *pEntry;

    pDir = opendir(pDirName);
    if (!pDir)
    {
            perror("opendir");
            return -1;
    }

    while ((pEntry = readdir(pDir)) != NULL)
    {
            printf("%s\n", pEntry->d_name);
    }

    closedir(pDir);
    return 0;
}
于 2013-10-05T16:13:48.643 回答