繁体   English   中英

在 C Linux 中递归创建文件

[英]Create file recursive in C Linux

我想在这个路径/tmp/a1/a2/a3/a4/test中创建文件test

但是所有目录(a1..a4)都不存在,如何在 Linux OS 的 C 中创建此文件?

您可以使用sys/stat.h中的mkdir function 根据需要创建目录。 例如,

mkdir("/tmp/a1",0766);

但是,您应该通过stat检查目录是否已经存在。

创建目录结构后,可以使用以下命令创建文件

open("/tmp/a1/a2/a3/a4/test",O_WRONLY|O_CREAT);

显然,您需要检查所有这些 function 调用的返回值。

下面是一个完整的 C 中的C ,可以实现您想要的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>

int create_file_with_parent_dirs (char pathname[])
{
    for (char *p = pathname; (p = strchr(p ,'/')) != NULL; ++p) {
        char c = p[1];
        p[1] = '\0';
        errno = 0;
        if (mkdir(pathname, 0700) != 0 && errno != EEXIST) {
            perror("mkdir");
            return -1;
        }
        p[1] = c;
    }
    int fd = creat(pathname, 0600);
    if (fd < 0)
        perror("creat");
    return fd;
}

int main (void)
{
    char pathname[] = "/tmp/a1/a2/a3/a4/test";
    create_file_with_parent_dirs(pathname);
}

请注意, pathname指向的数组必须是可修改的。 不要使用字符串文字调用 function。 另请注意,如果文件已经存在,它将被截断为零长度。

您可以使用此代码。 该程序拆分路径并检查路径是否存在,如果不创建路径并将最终路径创建为文件。

 #include <dirent.h>
 #include <errno.h>
 #include <bits/stdc++.h>
 #include <iostream>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <vector>
 #include <fstream>
 using namespace std;
 int checkDir(char * path)
 {
    DIR* dir = opendir(path);
    if (dir) {
            
            /* Directory exists. */
            closedir(dir);
            return 0;
    } else if (ENOENT == errno) {
            /* Directory does not exist. */
            return -1;
    } else {
            /* opendir() failed for some other reason. */
            return -2;
    }
 }
 int make_dir(char *path)
 {
    int ret = checkDir(path);
    if( ret == -1)
         {
                 if (mkdir(path, 0777) == -1)
            {
                         cerr << "Error :  " << strerror(errno) << endl;
                    return -1;
            }
 
                 else
            {
                         cout << "Directory created";
                    return 0;
            } 
         }
    return ret;
 
 }
 int main(int args, char **argv)
 {
    std::string strpath(argv[1]);
    std::string delimeter = "/";
    std::string substr1 = "";
    int cnt = 0;
    std::vector<string> strPaths;
    std::string strbck = strpath;

            for( int i = strpath.find(delimeter);  i != std::string::npos; i = strpath.find(delimeter))
    {
            if(cnt > 0)
            {
                    substr1 = strbck.substr(0,  substr1.length() + i + 1);
                    strPaths.push_back(substr1);
            }

            strpath = strpath.substr(i +1,strpath.length());
            cnt++;
    }
    strPaths.push_back(strbck);
    std::string str;

    for_each( strPaths.begin() ,strPaths.end() -1 , [](std::string str) {make_dir((char*)str.c_str());});
    ofstream outfile;
    std::ofstream file {strPaths[strPaths.size() -1].c_str() };
    file << "hello"<< std::endl;
    file.close();
                    return 0;
 }

暂无
暂无

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

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