简体   繁体   中英

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

My goal is to, inside my C program, make a new directory. Within this directory, I want to create 15 simple text files. The part that I am stuck on is how to generate the 15 text files inside the newly created directory. 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. The C11 standard (read n1570 ) does not know about directories (an abstraction provided by your operating system ). If you are on Windows, it has a different WinAPI (you should then use CreateDirectory )

First, your call to mkdir(2) could fail (for a variety of reasons, including the fact that the directory did already exist). 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). Practically speaking, the directory path should be computed at runtime as a string (perhaps using snprintf(3) or 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:

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 ). I am leaving these checks as an exercise. You might want to keep the result of getenv("HOME") in some automatic variable .

Then you need to make the directory, and check against failure. At the very least (using perror(3) and see 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 ). So I prefer 0750 to yours 0755 .

At last you need to create files inside it, perhaps using fopen(3) before writing into them. 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.

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) ).

If on Linux, you'll better read some system programming book like ALP or newer. If you use a different OS, you should read the documentation of its system API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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