繁体   English   中英

检查C ++中是否存在文件

[英]Check if file exists in C++

我对C ++非常陌生。 在我目前的项目中,我已经包括在内

#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>

我只需要在main()的最开头快速检查一下,看看我的程序目录中是否存在必需的dll。 那么对我来说最好的方法是什么呢?

因此,假设只需在同一目录中检查具有正确名称EXISTS的文件即可:

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM