C 操作目录 : how to position at a directory by giving its name in main arguments

标签 c directory argv opendir

我在操作 C 中的目录时遇到问题。

  1. 我想给出 2 个目录的名称作为 main 的参数
  2. 检查第一个目录是否存在(在当前路径中)
  3. 打开目录
  4. 调用一个函数(我创建的)来创建文件并在里面做一些事情 目录
  5. 关闭目录并进入第二个目录并执行相同的操作。

我写了我的代码,但它仍然没有执行我在 main 上给出的目录中的内容,相反,它看起来像我总是位于当前目录中,所以打开目录的调用不好???

这是我所做的:

 int main(int argc, char *argv[])
    {
       int i = 0;
       char cwd[1024];
       if(argc < 3)
       {
            printf("Erreur dans les arguments\n");
       } else 
       {
          for(i = 1; i < argc; i++)
          {
            if (getcwd(cwd, sizeof(cwd)) == NULL)
            {
                printf("an error occured when getting current directory\n");
            }
            // make a path to the directory
            strcat(cwd, "/");
            strcat(cwd, argv[i]);
            strcat(cwd, "/");
            printf("cwd %s\n", cwd);
            //check if directory exist and readable
            //if((rep = opendir(argv[i])) != NULL) not working also
            if((rep = opendir(cwd)) != NULL)
            {
               getcwd(cwd, sizeof(cwd));
               printf("cwd %s\n", cwd);

              // do some stuff on the directory 

              //int result = createFile("file.txt"); // this function works fine but the file is always created in the current directory
            }
        }
     }
   }

如果有人能提供帮助,我们将不胜感激。谢谢你。

最佳答案

这是一些工作代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>

int main(int argc, char **argv)
{
    int i = 0;
    char path[1024];

    if (argc < 3)
    {
        fprintf(stderr, "Usage: %s dir1 dir2 [...]\n", argv[0]);
        exit(1);
    }

    for (i = 1; i < argc; i++)
    {
        DIR *rep;
        if ((rep = opendir(argv[i])) != NULL)
        {
            struct dirent *dp;
            while ((dp = readdir(rep)) != 0)
            {
                if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
                    continue;
                printf("Name: %s/%s\n", argv[i], dp->d_name);
            }
            closedir(rep);

            snprintf(path, sizeof(path), "%s/%s", argv[i], "filename.txt");
            FILE *fp = fopen(path, "w");

            if (fp == 0)
                fprintf(stderr, "Failed to create file %s\n", path);
            else
            {
                fprintf(fp, "File %s created successfully\n", path);
                fclose(fp);
            }
        }
    }
    return 0;
}

运行示例:

$ mkdir junk1 junk1/subdir junk2 junk2/subdir-too
$ cp /dev/null junk1/forget-me-not
$ cp /dev/null junk2/hallelujah-chorus
$ ./dodir junk1 junk2
Name: junk1/forget-me-not
Name: junk1/subdir
Name: junk2/hallelujah-chorus
Name: junk2/subdir-too
$ ls -l junk?
junk1:
total 8
-rw-r--r--  1 jleffler  staff  45 Oct 25 00:11 filename.txt
-rw-r--r--  1 jleffler  staff   0 Oct 25 00:11 forget-me-not
drwxr-xr-x  2 jleffler  staff  68 Oct 25 00:11 subdir

junk2:
total 8
-rw-r--r--  1 jleffler  staff  45 Oct 25 00:11 filename.txt
-rw-r--r--  1 jleffler  staff   0 Oct 25 00:11 hallelujah-chorus
drwxr-xr-x  2 jleffler  staff  68 Oct 25 00:11 subdir-too
$ rm -fr junk?
$

关于C 操作目录 : how to position at a directory by giving its name in main arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33326565/

相关文章:

c - 从 int、float、char、double 进行类型转换

c# - qt 到 c# : operator mean within struct 是什么

c++ - Visual Studio 在 vmware 中与 gcc/gdb 交叉编译 -> 内存访问错误

c - 创建目录时出现段错误(核心转储)

c - 在 C 中使用 argv 和 argc 解析命令行

c - C中的指针混淆

SVN 提交前撤消删除

c - 为什么在通过命令行参数传递时,字符串中的十六进制未转换为十六进制?

c++ - 检查 argv[i] 是否为有效整数,在 main 中传递参数

windows - 如何创建授予所有人所有权限的目录