[英]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: 一些评论:
PATH_MAX
and FILE_MAX
in limits.h
limits.h
中包含宏PATH_MAX
和FILE_MAX
。 closedir
function receives a DIR *
argument, which I suppose would be dir
in your code. closedir
函数也会收到一个DIR *
参数,我想这将是您代码中的dir
。 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. 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.