Ç操纵目录:如何在主要论点给出它的名字在目录中的位置 [英] C manipulate directories : how to position at a directory by giving its name in main arguments

查看:152
本文介绍了Ç操纵目录:如何在主要论点给出它的名字在目录中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法操纵目录用C


  1. 我想给的2个目录的名称作为主
  2. 参数
  3. 检查,如果第一目录存在(当前路径)

  4. 打开目录

  5. 调用一个函数(我创建)创建文件,里面做的东西
    目录

  6. 关闭该目录,并进入第二个目录,并做相同的。

我写我的code,但还是没有这样做,我给上主目录里面的东西,而不是它看起来像我总是定位在当前目录下,所以是开不好目录通话???

I wrote my code but it still not doing the stuffs inside the directories that i gave on main, instead it looks like i'm always positioned in the current directory, so is the call to open the directory not good???

下面是我做了什么:

 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
            }
        }
     }
   }

如果有人可以帮助,这将是AP preciated。感谢ü。

if anyone could help, it will be appreciated. Thank u.

推荐答案

下面是一些工作code:

Here's some working code:

#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?
$

这篇关于Ç操纵目录:如何在主要论点给出它的名字在目录中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆