C manipulate directories : how to position at a directory by giving its name in main arguments

吃可爱长大的小学妹 提交于 2019-12-11 19:42:50

问题


I'm having trouble to manipulate directories in C.

  1. I want to give the name of 2 directories as argument on main
  2. check if the first directory exists (in the current path)
  3. open the directory
  4. call a function (that i created) to create files and do stuff inside the directory
  5. close the directory and go into the 2nd directory and do the same .

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

Here's what I've done :

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

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


回答1:


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

Example run:

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



回答2:


opendir doesn't change the current working directory. It opens up a directory and gives you a handle to it.

You need to call chdir to actually change the current working directory.



来源:https://stackoverflow.com/questions/33326565/c-manipulate-directories-how-to-position-at-a-directory-by-giving-its-name-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!