Creating Directory structure

This is a discussion on Creating Directory structure within the C Programming forums, part of the General Programming Boards category; Hi I am using the following code to tokenise a string which contains a relative directory and then create the ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    9

    Creating Directory structure

    Hi I am using the following code to tokenise a string which contains a relative directory and then create the directory structure. Works fine for stuff like folder/folder1/folder2, but doesn't work for such directories as ../../folder/folder2. Any ideas how I can modify it to work for strings like that?

    Code:
    v
    void createdirstruct(char *folder) {
      char	tokfolder[512];
      int	i;
      
      for (i = 0; i <= strlen(folder); i++) {
        if (folder[i] == '/' || folder[i] == '\0') {
          strncpy(tokfolder, folder, i);
          tokfolder[i] = '\0';
          if(mkdir(tokfolder, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) {
    	printf("Restoring folder structure failed.\n");
    	exit (13);
     
          }
        } 
      }
    }
    Thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Just guessing here... but maybe by adding more logic?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And you'll have to be more explicit about what you mean by "works". So far as I can tell, all this code does is turn "folder/folder1/folder2" into "folder" and create a directory of that name. It doesn't continue on with the string. It doesn't deal with any of the other directories.

    And trying to make the directory ".." is going to fail SO hard.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    Forgot to copy the chdir bit. Erm well if I pass the function folder/folder1/folder2 It creates those folders just like that, one inside another. Just need a way to test if there is ../ and chdir instead of creating

    Code:
    void createdirstruct(char *folder) {
      char	tokfolder[512];
      int	i;
      
      for (i = 0; i <= strlen(folder); i++) {
        
        if (folder[i] == '/' || folder[i] == '\0') {
          strncpy(tokfolder, folder, i);
          tokfolder[i] = '\0';
          if(mkdir(tokfolder, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) {
    	printf("Restoring folder structure failed.\n");
    	exit (13);
          }
          if(chdir(tokfolder) == -1) {
    	  printf("Directory change to %s failed\n",tokfolder);
    	  exit (11);
          }
        } 
      }
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay, so your tokenizing thing works, fine. Why not stat the name to see if it exists before blithely trying to create it? (I.e., this won't just fail on "..", but on "folder/" if that directory already exists.)

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    Yeah I think I will do that now. I've just got my order messed up. I used stat on the full string before this function is called, but obviously like you said I need to do it for each token

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 05:53 PM
  2. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  3. Creating a new directory
    By harryP in forum C++ Programming
    Replies: 1
    Last Post: 10-05-2003, 10:06 PM
  4. capturing directory structure
    By vivek_kumbhar in forum C Programming
    Replies: 2
    Last Post: 11-03-2002, 12:24 AM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 08:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21