检查c++中是否存在file
Check if file exists in C++
我对c++非常非常陌生。在我当前的项目中,我已经包含了
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
,我只需要在main()的开头做一个快速检查,看看我的程序目录中是否存在所需的dll。那么对我来说什么是最好的方法呢?
那么,假设可以简单地检查具有正确名称的文件是否存在于同一目录中:
#include <fstream>
...
void check_if_dll_exists()
{
std::ifstream dllfile(".\myname.dll", std::ios::binary);
if (!dllfile)
{
... DLL doesn't exist...
}
}
如果你想知道它实际上是一个真正的DLL(而不是某人打开命令提示符并执行type NUL: > myname.dll
来创建一个空文件),你可以使用:
HMODULE dll = LoadLibrary(".\myname.dll");
if (!dll)
{
... dll doesn't exist or isn't a real dll....
}
else
{
FreeLibrary(dll);
}
有很多方法可以实现这一点,但使用boost库始终是一种好方法。
#include <boost/filesystem.hpp>
using boost::filesystem;
if (!exists("lib.dll")) {
std::cout << "dll does not exists." << std::endl;
}
相关文章:
- C++模板来检查友元函数的存在
- Seg Fault Issue C++ (file IO / getline)
- 既然存在危险,为什么项目要使用-I include开关
- 我们可以访问一个不存在的联盟的成员吗
- C++:对不存在的命名空间使用命名空间指令
- C++quit()函数中可能存在作用域问题
- C++擦除(如果存在)
- g++ 说函数不存在,即使包含正确的标头
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 有了gcc,是否可以链接库,但前提是它存在
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- C++Builder中的OnClick事件签名存在问题
- GetPrivateProfileStringA 返回"The system cannot find the file specified."即使文件存在
- 成功使用CMakeLists制作后如何修复"open: No such file or directory"错误(该文件实际上存在)
- unlink() 在检查存在后立即失败并显示"No such file or directory"
- 必须包含存在 file.h "#include "的所有"file.o"文件" "
- 检查c++中是否存在file
- 检查Windows安装盘中是否存在file,是否已连接
- 没有这样的文件或目录,只有file存在