简体   繁体   English

创建目录和存储文件(C编程)

[英]Create Directory and Store File (C programming)

I want to create a directory that has the name ends with the process ID (to make it unique) and then store the new files that I just wrote inside that directory. 我想创建一个目录,该目录的名称以进程ID结尾(以使其唯一),然后将我刚刚写入的新文件存储在该目录中。

What I want: 我想要的是:

1) Create a new directory named : mydirectory.1923 (example of the process id number) 1)创建一个名为: mydirectory.1923的新目录(进程ID号示例)

2) Store a file that I just created using 2)存储我刚刚使用创建的文件

FILE * fPointer = fopen("new.txt",w+)

into mydirectory.1923 进入mydirectory.1923

What I have so far is this: 我到目前为止所拥有的是:

int bufSize = 20;
int pid = getpid();
char *fileName = malloc(bufSize);
char *prefix = "that.rooms.";
snprintf(fileName, bufSize,"%s%d", prefix, pid);
printf("%s\n",fileName);
struct stat st = {0};
if (stat(fileName, &st) == -1) {
    mkdir(fileName, 0755);
}
DIR *dir = opendir (fileName);
if (dir != NULL) {

    FILE *fLib = fopen("library.txt" , "w+");
    fclose(fLib);

}
closedir(fileName);

return 0;

My Question: 我的问题:

This code doesn't work, apparently it says error on the DIR part. 该代码不起作用,显然它在DIR部分显示错误。

Is this the right thing to do if I want to create directory, create file and store that file directly to the new directory? 如果要创建目录,创建文件并将该文件直接存储到新目录,这是正确的做法吗?

Is there any suggestion or advice to do it better than this? 有什么建议或建议可以做得更好吗? Thank you. 谢谢。

Some comments: 一些评论:

  • As commented above, you should really allocate more space for your dir's name, a 5 digit pid will break your code right away. 如上所述,您确实应该为目录名称分配更多空间,一个5位数的pid将立即破坏您的代码。 There are the macros PATH_MAX and FILE_MAX in limits.h limits.h中包含宏PATH_MAXFILE_MAX
  • You don't need to open the directory for anything. 您无需打开任何目录。 This is usually used to iterate over items in directories. 这通常用于遍历目录中的项目。 You only want to create a file in it. 您只想在其中创建一个文件。
  • Even if you don't really need it, the closedir function receives a DIR * argument, which I suppose would be dir in your code. 即使您并不是真的需要它, closedir函数也会收到一个DIR *参数,我想这将是您代码中的dir
  • To create the file inside the new directory you should include your dir in the path on creation, or at least chdir to it before the fopen call. 要在新目录中创建文件,应在创建路径中包含目录,或者在fopen调用之前至少将其包含在chdir中。 I don't recommend the latter as it affects the process wide working directory. 我不建议使用后者,因为它会影响整个进程的工作目录。

Below is a quick and dirty patched version of your code that creates the directory and the new file inside it taking into account the above: 以下是您的代码的快速且肮脏的修补版本,它考虑到上述因素,创建了目录和其中的新文件:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <limits.h>

int main(int argc, char **argv){
    int pid = getpid();
    char dirName[NAME_MAX+1];
    char *prefix = "that.rooms.";
    snprintf(dirName, NAME_MAX + 1,"%s%d", prefix, pid);
    printf("%s\n",dirName);
    struct stat st = {0};
    if (stat(dirName, &st) == -1) {
        if(mkdir(dirName, 0755) != -1){
            char libPath[PATH_MAX+1];
            snprintf(libPath, PATH_MAX + 1, "%s/library.txt", dirName);

            FILE *fLib = fopen(libPath , "w+");
            fclose(fLib);
        }else{
            perror("mkdir: ");
        }
    }
    return 0;
}

Changed the variable names a bit so it's clearer: 更改了变量名称,使它更清晰:

  • dirName is used to hold the directory name. dirName用于保存目录名称。 It uses NAME_MAX as this is the system limit for the length of a file name 它使用NAME_MAX因为这是文件名长度的系统限制
  • libPath is used to hold the path to the library.txt file you are creating. libPath用于保存要创建的library.txt文件的路径。 If your pid was 3123, libPath would read that.rooms.3123/library.txt after the snprintf call. 如果您的pid是3123,则libPath会在snprintf调用后读取that.rooms.3123/library.txt It uses PATH_MAX as this is the system limit for a the length of a file's path. 它使用PATH_MAX因为这是文件路径长度的系统限制。

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

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