gpt4 book ai didi

c++ - 在 Windows 上使用 C++ 删除文件

转载 作者:可可西里 更新时间:2023-11-01 09:47:33 25 4
gpt4 key购买 nike

我正在编写一个 C++ 程序,如果满足某些条件,我想删除一些已创建的文件。这些文件具有各种文件名,在每次迭代中使用类型“字符串”分配这些文件名。现在我试图删除一些文件名,但似乎 Deletefile 函数和 remove 都不能处理 C++ 字符串。我也尝试过将字符串转换为 c 类型的 char* 但它不起作用。

我在 Windows 10 上使用 visual studio community 2015。

他们有什么方便的方法来解决这个问题吗?

最佳答案

如评论中所述 - 函数 .c_str() 返回可与 DeleteFileremove 一起使用的 C 兼容字符串。

如果这不起作用,我猜你的应用程序被编译为 Unicode ,这意味着你将不得不使用 std::wstring 而不是 std::string.

尝试结合这两种方式:

std::string fileName = "C://file.txt";
std::wstring wFileName(fileName.begin(),fileName.end());
auto res = DeleteFile(wFileName.c_str());

remove 但是,使用“常规”const char*

关于c++ - 在 Windows 上使用 C++ 删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916810/

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