创建新文件夹(如果fopen不存在) [英] Creating new folders if they don't exist for fopen

查看:1150
本文介绍了创建新文件夹(如果fopen不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++程序,该程序需要用户输入fopen以便启动文件写入.有人可以帮助我找到一个函数,该函数将返回FILE*并使用Windows特定版本的mkdir,以便为fopen创建文件夹结构,以使它永远不会在指定位置打开新文件,因为其中之一该文件夹不存在.谢谢你!

I have a C++ program that takes user input for fopen in order to initiate a file write. Could someone help me find a function which will return a FILE* and use the Windows specific version of mkdir in order to create the folder structure for fopen to never fail to open a new file in the specified location because one of the folders does not exist. Thanks a bunch!

推荐答案

windows API中有一个MakeSureDirectoryPathExists方法,该方法在dbghelp.h中声明.它以递归方式创建目录,所以我想这就是您所追求的. 但是,无法确保按照您的要求确保此永不失败",因为如果您具有对特定目录的写访问权,它也取决于特权等.

there's a method MakeSureDirectoryPathExists in the windows API, declared in dbghelp.h. It recursively creates directories, so I guess that's what you are after. However, there is NO way of making sure this 'never fails' as you ask, as it also depends on privileges etc if you have write access to a certain directory.

edit:这是一些虚拟的示例代码;它使用GetProcAddress,因为在编写它时找不到dbghelp标头.

edit: here's some dummy sample code; it uses GetProcAddress though, as I couldn't find the dbghelp header when I wrote it.

typedef BOOL (WINAPI * CreateDirFun ) ( __in PCSTR DirPath );

HMODULE h = LoadLibrary( "dbghelp.dll" );
CreateDirFun pFun = (CreateDirFun) GetProcAddress( h, "MakeSureDirectoryPathExists" );
(*m_pFun)( psPath ) )
CreateDirectory( psPath );
FreeLibrary( h );

这篇关于创建新文件夹(如果fopen不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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