创建在C新目录 [英] Creating a new directory in C

查看:116
本文介绍了创建在C新目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个程序,检查目录的存在;如果该目录不存在,那么它创建的目录和它里面的日志文件,但如果该目录已经存在,那么它只是创建一个文件夹中的一个新的日志文件。

I want to write a program that checks for the existence of a directory; if that directory does not exist then it creates the directory and a log file inside of it, but if the directory already exists, then it just creates a new log file in that folder.

我将如何使用Linux做这在C?

How would I do this in C with Linux?

推荐答案

与stat 检查,如果存在的目录,

Look at stat for checking if the directory exists,

的mkdir ,创建一个目录。

And mkdir, to create a directory.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

struct stat st = {0};

if (stat("/some/directory", &st) == -1) {
    mkdir("/some/directory", 0700);
}

您可以看到与人2统计这些功能的手动男子2的mkdir 命令。

这篇关于创建在C新目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆