Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Why does the following recursive directory creation fail?
Qt 6.11 is out! See what's new in the release blog

Why does the following recursive directory creation fail?

Scheduled Pinned Locked Moved C++ Gurus
2 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lalalo
    wrote on last edited by
    #1

    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.
    @#include<iostream>
    #include<stdlib.h>

    #include<windows.h>
    #include<stdio.h>

    void make_dir(int depth, char *dir) {
    if (depth < 4) {
    CreateDirectoryA (dir,NULL);
    for (int i = 0; i < 10; i++) {
    char sdir= (char)malloc(strlen(dir+10)); // XXX 10?
    strcpy(sdir, dir);
    printf("%s\n", sdir);
    CreateDirectoryA(sdir,NULL);
    make_dir(depth + 1, sdir);
    free(sdir);
    }
    }

    }

    int main()
    {
    make_dir(0,"dir");
    return 1;
    }@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

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

      @
      QString filePath = QString("f/%1/%2/%3/%4/%5")
      .arg(dirHash1)
      .arg(dirHash2)
      .arg(dirHash3)
      .arg(dirHash4)
      .arg(fileName);

      QFileInfo fi(filePath);
      QDir dir = fi.absoluteDir();
      if(!dir.exists())
      dir.mkpath(fi.absoluteDirPath());

      QFile file(filePath);
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved