如何在c中创建以下要求的递归目录? [英] how do i create recursive directories for the following requirement in c?

查看:167
本文介绍了如何在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.

我希望目录结构能够分支出10个子目录,子目录的级别将为4.因为文件名保证是唯一的,我想使用这些文件名来创建可用于将文件放在目录中的哈希值,以后再找到它们。随机哈希值将使一个目录具有大约1,000个文件。

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.

所以如果F是根目录,那么插入或搜索文件将不得不经过以下步骤:

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

我想使用0-9的数字作为目录名

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

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

我如何创建这些董事?

编辑:

所有文件都是文本文件。
该计划将分发给许多人,以收集研究信息。所以tt很重要,这些文件是这样创建的。

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.

编辑:

我创建了以下代码实施 perreal的伪代码。它编译成功,但给出最后给出的运行时错误。
错误发生在 sprintf()行。

i created the following code to implement perreal's pseudo code. It compiles to success but gives the run time error given at the end. error occurs at the sprintf() line.

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




0x5b9c1cee处的未处理的异常(msvcr100d.dll) in mkdir.exe:
0xC0000005:访问冲突写入位置0x00be5898。

Unhandled exception at 0x5b9c1cee (msvcr100d.dll) in mkdir.exe: 0xC0000005: Access violation writing location 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);
    }   
  }     
}

}

并调用 make_dir(0,rootdir);

这篇关于如何在c中创建以下要求的递归目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆