检查文件是否在C ++中存在 [英] Check if file exists in C++
问题描述
我对C ++非常陌生. 在我当前的项目中,我已经包含了
I'm very very new to C++. In my current project I already included
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
,我只需要在main()的开头进行快速检查,以查看程序目录中是否存在必需的dll. 那么对我来说最好的方法是什么?
and I just need to do a quick check in the very beginning of my main() to see if a required dll exists in the directory of my program. So what would be the best way for me to do that?
推荐答案
因此,假设可以在同一目录中简单地检查名称正确的文件是否存在,就可以了:
So, assuming it's OK to simply check that the file with the right name EXISTS in the same directory:
#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
创建一个空文件的人),则可以使用:
If you want to know that it's ACTUALLY a real DLL (rather than someone opening a command prompt and doing type NUL: > myname.dll
to create an empty file), you can use:
HMODULE dll = LoadLibrary(".\\myname.dll");
if (!dll)
{
... dll doesn't exist or isn't a real dll....
}
else
{
FreeLibrary(dll);
}
这篇关于检查文件是否在C ++中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!