March 2, 2012

lalalo lalalo
Lab Rat
1 posts

why does the following recursive directory creation fail?

 

i have posted this same question on stackoverflow and i got as far as shown. But still i have some errors(synactical and logical) which i couldn’t solve at all.
May be i can get some help here;

i expect to have more than one million files with unique names. I have been told that if i put all this files in one or two directories the search speed for these files will be extremely slow. So i have come up with the following directory architecture.

I want the directory structure to branch out with 10 sub directories and the level of the sub directories will be 4. because the file names are guaranteed to be unique i want to use these file names to make hashes which can be used to put the file in a directory and also later to find it. The random hash values will make a directory to have,approximately, 1,000 files.

so if F is root directory then inserting or searching for a file will have to go through these steps:

I want to use numbers from 0-9 as directory names

h=hash(filename)
sprintf(filepath,“f//%d//%d//%d//%d//.txt”,h,h,h,h);
HOW DO I CREATE THESE DIRECTORIES?

EDIT:

All the files are text files. The program will be distributed to many people in order to collect information for a research. So tt is important that these files are created like this.

  1. #include<iostream>
  2. #include<stdlib.h>
  3.  
  4. #include<windows.h>
  5. #include<stdio.h>
  6.  
  7. void make_dir(int depth, char *dir) {
  8.   if (depth < 4) {
  9.    CreateDirectoryA (dir,NULL);
  10.       for (int i = 0; i < 10; i++) {
  11.         char *sdir= (char*)malloc(strlen(dir+10)); // XXX 10?
  12.         strcpy(sdir, dir);
  13.         printf("%s\n", sdir);
  14.         CreateDirectoryA(sdir,NULL);
  15.         make_dir(depth + 1, sdir);
  16.         free(sdir);
  17.       }  
  18.     }  
  19.  
  20.   }
  21.  
  22. int  main()
  23. {
  24.  make_dir(0,"dir");
  25.  return 1;
  26. }

1 reply

March 2, 2012

Volker Volker
Ant Farmer
5428 posts

Being in a Qt forum, I come up with a Qt base solution:

  1. QString filePath = QString("f/%1/%2/%3/%4/%5")
  2.     .arg(dirHash1)
  3.     .arg(dirHash2)
  4.     .arg(dirHash3)
  5.     .arg(dirHash4)
  6.     .arg(fileName);
  7.  
  8. QFileInfo fi(filePath);
  9. QDir dir = fi.absoluteDir();
  10. if(!dir.exists())
  11.     dir.mkpath(fi.absoluteDirPath());
  12.  
  13. QFile file(filePath);

 
  ‹‹ Error handling (try catch)      Support std::shared_ptr<> ››

You must log in to post a reply. Not a member yet? Register here!