gpt4 book ai didi

c++ - 如何在没有文件名的情况下获取文件本身的目录?

转载 作者:行者123 更新时间:2023-11-30 04:58:41 27 4
gpt4 key购买 nike

我正在尝试获取文件的文件名(目录)并创建其子目录。但是如果我的文件的扩展名是.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 ==========

最佳答案

这是一个使用 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);

关于c++ - 如何在没有文件名的情况下获取文件本身的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51585704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com