简体   繁体   English

C编程:如何创建父目录并手动插入文件?

[英]C Programming: How to create a parent directory and insert files manually?

My goal is to, inside my C program, make a new directory. 我的目标是在我的C程序中创建一个新目录。 Within this directory, I want to create 15 simple text files. 在此目录中,我想创建15个简单的文本文件。 The part that I am stuck on is how to generate the 15 text files inside the newly created directory. 我停留的部分是如何在新创建的目录中生成15个文本文件。 I have created the new directory like this: 我创建了这样的新目录:

mkdir("new_dir", 0755);

But I am unsure of how to create a text file within it (in the same program). 但是我不确定如何在其中创建一个文本文件(在同一程序中)。 Any tips for this? 有什么提示吗?

I am guessing you are on some POSIX system. 我猜您在某些POSIX系统上。 The C11 standard (read n1570 ) does not know about directories (an abstraction provided by your operating system ). C11标准(读取n1570 )不了解目录操作系统提供的抽象 )。 If you are on Windows, it has a different WinAPI (you should then use CreateDirectory ) 如果您使用的是Windows,则它具有不同的WinAPI (然后应使用CreateDirectory

First, your call to mkdir(2) could fail (for a variety of reasons, including the fact that the directory did already exist). 首先,对mkdir(2)的调用可能会失败(由于多种原因,包括目录已经存在的事实)。 And very probably, you actually want to create the directory in the home directory, or document that you are creating it in the current working directory (eg leave the burden of some appropriate and prior cd shell builtin to your user). 很可能您实际上是想在主目录中创建目录,或在当前工作目录中创建要创建的文档(例如,将一些适当的和先前的cd shell的负担留给用户)。 Practically speaking, the directory path should be computed at runtime as a string (perhaps using snprintf(3) or asprintf(3) ). 实际上,目录路径应在运行时作为字符串计算(也许使用snprintf(3)asprintf(3) )。

So if you wanted to create a directory in the home directory of the user (remember that ~/foo/ is expanded by the shell during globbing, see glob(7) ...; you need to fetch the home directory from environ(7) ), you would code something like: 因此,如果您想在用户的主目录中创建目录(请记住, ~/foo/在globing期间被外壳程序扩展了,请参见glob(7) ...;您需要从environ(7)获取主目录。 ),您将编写类似以下内容的代码:

char pathbuf[256];
snprintf(pathbuf, sizeof(pathbuf), "%s/new_dir", getenv("HOME"));

to compute that string. 计算该字符串。 Of course, you need to handle failure (of getenv(3) , or of snprintf ). 当然,您需要处理失败( getenv(3)snprintf )。 I am leaving these checks as an exercise. 我把这些支票留作练习。 You might want to keep the result of getenv("HOME") in some automatic variable . 您可能需要将getenv("HOME")的结果保留在一些自动变量中

Then you need to make the directory, and check against failure. 然后,您需要创建目录,并检查是否失败。 At the very least (using perror(3) and see errno(3) ): 至少(使用perror(3)并查看errno(3) ):

 if (mkdir (pathbuf, 0750)) { perror(pathbuf); exit(EXIT_FAILURE); }

BTW, the mode passed to mkdir might not allow every other user to write or access it (if it did, you could have some security vulnerability ). 顺便说一句,传递给mkdir的模式可能不允许所有其他用户写入或访问它(如果这样做,则可能会有一些安全漏洞 )。 So I prefer 0750 to yours 0755 . 所以我更喜欢0750而不是您的0755

At last you need to create files inside it, perhaps using fopen(3) before writing into them. 最后,您需要在其中创建文件,也许在写入文件之前使用fopen(3) So some code like 所以一些代码像

int i = somenumber();
snprintf(pathbuf, sizeof(pathbuf), 
         "%s/new_dir/file%d.txt", getenv("HOME"), i);
FILE* f = fopen(pathbuf, "w");
if (!f) { perror(pathbuf); exit(EXIT_FAILURE); };

As Jonathan Leffler wisely commented, there are other ways. 正如乔纳森·莱夫勒(Jonathan Leffler)明智地指出的那样,还有其他方法。

My recommendation is to document some convention. 我的建议是记录一些约定。 Do you want your program to create a directory in the current working directory, or to create it in some "absolute" path, perhaps related to the home directory of your user? 您是要程序在当前工作目录中创建目录还是在某个“绝对”路径中创建目录,也许与用户的主目录有关? If your program is started by some user (and is not setuid or doesn't have root permissions, see credentials(7) ) it is not permitted to create directories or files at arbitrary places (see hier(7) ). 如果您的程序是由某些用户启动的(不是setuid或没有root权限,请参见凭据(7) ),则不允许在任意位置创建目录或文件(请参见hier(7) )。

If on Linux, you'll better read some system programming book like ALP or newer. 如果在Linux上,则最好阅读一些系统编程书籍,例如ALP或更高版本。 If you use a different OS, you should read the documentation of its system API. 如果使用其他操作系统,则应阅读其系统API的文档。

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

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