如何获取不带文件名的文件本身的目录?

时间:2018-07-30 00:40:06

标签: c++ console-application

我正在尝试获取文件的文件名(目录)并使其成为子目录。但是,如果我文件的扩展名是.exe,我想删除8个字符以完全删除安装程序中的“ test.exe”文本,然后加入// test(_tcscat(installer,_T(“ \ test”));)是否可以?也许可以在没有文件名的情况下获取该路径的文件夹名称?

 int _tmain(int argc, _TCHAR* argv[])
    {
        TCHAR buf0[MAX_PATH];
        GetModuleFileName(0, buf0, MAX_PATH);

        TCHAR installer[_MAX_PATH];
        _tcscpy(installer, buf0);
        _tcscat(installer, _T("\\test"));
        _mkdir(installer);
    }

编辑:

#include <boost/filesystem.hpp>

using namespace boost::filesystem;

int main()
{

    TCHAR buf0[MAX_PATH];
    GetModuleFileName(0, buf0, MAX_PATH);
    boost::filesystem::path p(buf0);
    boost::filesystem::path dir = p.parent_path();
    TCHAR installer[_MAX_PATH];
    _tcscpy(installer, dir);
    _tcscat(installer, _T("\\test"));
    _mkdir(installer);


}

1>------ Build started: Project: ConsoleApplication6, Configuration: Release Win32 ------
1>  ConsoleApplication6.cpp
1>ConsoleApplication6.cpp(107): error C2664: 'char *strcpy(char *,const char *)' : cannot convert argument 2 from 'boost::filesystem::path' to 'const char *'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

#include <boost/filesystem.hpp>

using namespace boost::filesystem;

int main()
{

    TCHAR buf0[MAX_PATH];
    GetModuleFileName(0, buf0, MAX_PATH);
    boost::filesystem::path p(buf0);
    boost::filesystem::path dir = p.parent_path() / boost::filesystem::path("test");
    //boost::filesystem::create_directory(dir);
    char * dir0 = dir;
    char installer[_MAX_PATH];
    _tcscpy(installer, dir0);
    _tcscat(installer, _T("\\test"));


}

1>------ Build started: Project: ConsoleApplication6, Configuration: Release Win32 ------
1>  ConsoleApplication6.cpp
1>ConsoleApplication6.cpp(107): error C2440: 'initializing' : cannot convert from 'boost::filesystem::path' to 'char *'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:1)

以下是使用boost文件系统的示例:

TCHAR buf0[MAX_PATH];
GetModuleFileName(0, buf0, MAX_PATH);
boost::filesystem::path p(buf0);
boost::filesystem::path dir = p.parent_path() / boost::filesystem::path("test");    
boost::filesystem::create_directory(dir);