简体   繁体   English

如何在c中为以下要求创建递归目录?

[英]how do i create recursive directories for the following requirement in c?

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. 我希望目录结构分支出10个子目录,子目录的级别将是4.因为文件名保证是唯一的我想使用这些文件名来制作可用于放置文件的哈希在一个目录中,以后再找到它。 The random hash values will make a directory to have,approximately, 1,000 files. 随机哈希值将使目录具有大约1,000个文件。

so if F is root directory then inserting or searching for a file will have to go through these steps: 因此,如果F是根目录,那么插入或搜索文件将必须执行以下步骤:

I want to use numbers from 0-9 as directory names 我想使用0-9中的数字作为目录名称

h=hash(filename)
sprintf(filepath,"f//%d//%d//%d//%d//.txt",h%10,h%10,h%10,h%10);

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. 因此,重要的是这些文件是这样创建的。

EDIT: 编辑:

i created the following code to implement perreal's pseudo code. 我创建了以下代码来实现perreal的伪代码。 It compiles to success but gives the run time error given at the end. 它编译成功但会给出最后给出的运行时错误。 error occurs at the sprintf() line. sprintf()行发生错误。

#include<iostream>
#include<stdlib.h>

#include<windows.h>
void make_dir(int depth, char *dir) {
        if (depth < 4) {
               if (!  CreateDirectoryA (dir,NULL))
                for (int i = 0; i < 10; i++) {
                    sprintf(dir,"\\%d",i);
                    char *sdir=NULL ;
                        strcpy(sdir,dir);
                        CreateDirectoryA(sdir,NULL);
                        make_dir(depth + 1, sdir);
                }
        }
}
int  main()
{
    make_dir(0,"dir");
    return 1;
}

Unhandled exception at 0x5b9c1cee (msvcr100d.dll) in mkdir.exe: 0xC0000005: Access violation writing location 0x00be5898. mkdir.exe中0x5b9c1cee(msvcr100d.dll)的未处理异常:0xC0000005:访问冲突写入位置0x00be5898。

Kind of pseudo code, but can be done like this: 一种伪代码,但可以这样做:

 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);
        sprintf(sdir + strlen(sdir), "\\%d", i); 
        printf("%s\n", sdir);
        //CreateDirectoryA(sdir,NULL);
        make_dir(depth + 1, sdir);
        free(sdir);
    }   
  }     
}

} }

And to call make_dir(0, rootdir); 并调用make_dir(0, rootdir);

Do not do this: 不要这样做:

sprintf(dir,"\%d",i);

  1. dir is a const, read only string in your example. dir是示例中的const,只读字符串。
  2. You're likely to run off the end of the string, corrupting things that follow it in memory. 你可能会跑掉字符串的末尾,破坏内存中跟随它的东西。

Do not copy to sdir without allocating memory first. 不先分配内存,不要复制到sdir。

sdir = (char *)malloc( strlen( dir ) + 1 );

At the end of the function make_dir , you will have to call free( sdir ); 在函数make_dir ,你必须调用free( sdir ); so you do not leak memory. 所以你不要泄漏内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM