【问题标题】:Scanning a directory and modify file in c扫描目录并在c中修改文件
【发布时间】:2011-12-30 10:11:33
【问题描述】:

我正在尝试创建一个函数来扫描我的 Windows PC 上的文件夹,每次扫描时,都会在一个名为“Filter.txt”的文件中附加字符串“Test Script”。

现在的问题是2,第一个是扫描必须在目录c:\LOG或其子目录中执行,第二个是我不知道如何在目录中链接fopen和文件名。

int main(){
    DIR *dir;
    FILE * pFile;
    char myString[100];
    struct dirent *ent;
    dir = opendir ("c:\\LOG");
    if (dir != NULL) {
        /* print all the files and directories */
        while ((ent = readdir (dir)) != NULL) {
            pFile = fopen ("Filter.txt","a");
            if (pFile==NULL)
                perror("Error");  
            else
                fprintf(pFile,"%s\n","Test scriptIno");
            fclose(pFile);
            //printf ("%s\n", ent->d_name);
        }
        closedir (dir);
    } else {
        /* Can not open directory */
        perror ("");
        return EXIT_FAILURE;
    }
}

【问题讨论】:

  • 您不应该将 C 用于此类任务。更好地研究像 Perl 这样的脚本语言。每种语言/技术都是为特定目的而开发的。开发 C 是为了制作操作系统,而不是编写脚本。如果你有兴趣,我可以给你看一个 Perl 的例子。
  • @m0skit0:在 K&R 中,他们将 C 描述为“通用语言”。我没有找到“仅用于开发操作系统”之类的免责声明。
  • @undur_gongor:那是在 1972 年。现在你不想用 C 编写这样的脚本。除非您想真正学习 C,否则这只是浪费时间。这就是说,C 可能是我最喜欢的语言,但必须知道何时使用什么。
  • @m0skit0:不是 1978 年?虽然总的来说我同意,但可能有理由做这样的事情 C.
  • 是的,1978 年为 K&R,正确。 1972 年是 C 真正诞生的那一年。可能是有原因的,正如我在之前的评论中所说的那样,但是引入脚本语言是有原因的。

标签: c directory opendir readdir


【解决方案1】:

关于如何将调用链接到opendir,您可以在 SO 上找到大量答案,例如this。使用ent->d_type 来检查条目是目录还是文件。

要打开目录中的文件,只需使用ent->d_name 中的路径名来构造fopen 调用的路径。

编辑工作有点无聊,做了一个你可能想要的功能...

#ifdef _WIN32
# define DIR_SEPARATOR "\\"
#else
# define DIR_SEPARATOR "/"
#endif
void my_readdir(const char *path)
{
    DIR *dir = opendir(path);
    if (dir != NULL)
    {
        struct dirent *ent;

        static const char filtername[] = "filter.txt";

        /* +2: One for directory separator, one for string terminator */
        char *filename = (char *) malloc(strlen(path) + strlen(filtername) + 2);

        strcpy(filename, path);
        strcat(filename, DIR_SEPARATOR);
        strcat(filename, filtername);

        FILE *fp = fopen(filename, "a");

        while ((ent = readdir(dir)) != NULL)
        {
            if (ent->d_type == DT_REG || ent->d_type == DT_DIR)
            {
                if (strcmp(ent->d_name, "..") != 0 && strcmp(ent->d_name, ".") != 0)
                {
                    if (fp != NULL)
                        fprintf(fp, "%s : %s\n", (ent->d_type == DT_REG ? "File" : "Directory"), ent->d_name);

                    if (ent->d_type == DT_DIR)
                    {
                        /* +2: One for directory separator, one for string terminator */
                        char *newpath = (char *) malloc(strlen(path) + strlen(ent->d_name) + 2);

                        strcpy(newpath, path);
                        strcat(newpath, DIR_SEPARATOR);
                        strcat(newpath, ent->d_name);

                        /* Call myself recusively */
                        my_readdir(newpath);

                        free(newpath);
                    }
                }
            }
        }

        if (fp != NULL)
            fclose(fp);
        free(filename);
    }
}

编辑 看来opendirreaddir 函数在Windows 上的支持不是很好。以与我上面的示例类似的方式使用仅限 Windows 的 FindFirstFileFindNextFile。有关如何使用这些功能的示例,请参阅 this MSDN page

【讨论】:

  • 如果我创建这样的路径 myString = strcat(ent->d_type, "\\Filter.txt");编译器响应“结构没有名为 `d_type' 的成员”和“赋值中的类型不兼容”
  • @user1035523 ent->d_type 是文件的类型,而不是名称。而且您必须动态创建一个新字符串以包含路径的所有单独部分。
  • 非常感谢,但是当我运行函数编译器时,在“if (ent->d_type == DT_REG || ent- >d_type == DT_DIR)"
  • ..我还包括 dirent.h bat 没有任何改变。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-06-28
  • 2011-12-30
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
相关资源
最近更新 更多