创建文件夹/目录的跨平台方法? [英] Cross-platform method of creating a folder/directory?

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

问题描述

有没有一种方法可以使用跨平台的C语言以代码形式"创建文件夹/目录?还是我必须使用预处理器来说明要使用哪种方法?

Is there a way to create a folder/directory "in code" using C, that is cross-platform? Or will I have to use the preprocessor to state which method to use?

推荐答案

您将需要#define来完成此操作.

You'll need a #define to do this.

为了保持代码的外观整洁,您需要使用一个定义Linux函数的代码,以便在为Windows编译时转换为等效的Windows函数.

To keep your code looking clean, you'll want use one that will define the Linux function to translate to the equivalent Windows function when compiling for Windows.

在源文件的顶部,您将在Windows特定部分中找到它:

At the top of your source file, you'll have this in a Windows specific section:

#include <windows.h>

#define mkdir(dir, mode) _mkdir(dir)

然后,您可以像下面这样调用该函数:

Then later on you can call the function like this:

mkdir("/tmp/mydir", 0755);

以下是一些有用的内容:

Here are some others that might be useful:

#define open(name, ...) _open(name, __VA_ARGS__)
#define read(fd, buf, count) _read(fd, buf, count)
#define close(fd) _close(fd)
#define write(fd, buf, count) _write(fd, buf, count)
#define dup2(fd1, fd2) _dup2(fd1, fd2)
#define unlink(file) _unlink(file)
#define rmdir(dir) _rmdir(dir)
#define getpid() _getpid()
#define usleep(t) Sleep((t)/1000)
#define sleep(t) Sleep((t)*1000)

这篇关于创建文件夹/目录的跨平台方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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