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

查看:32
本文介绍了在 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);
}

您可以使用man 2 statman 2 mkdir 命令查看这些函数的手册.

You can see the manual of these functions with the man 2 stat and man 2 mkdir commands.

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

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