C++ 文件系统获取当前目录中的所有文件-Windows

C++ 文件系统获取当前目录中的所有文件-Windows,c++,windows,boost,filesystems,C++,Windows,Boost,Filesystems,我有一个qt应用程序,它使用以下实用程序函数获取给定目录路径中的文件列表 void listTopLevelFiles(const boost::filesystem::path& directoryPath, std::vector<std::string> &topLevelFiles) { if (!exists(directoryPath)) return; //non-recursive directory iterator

我有一个qt应用程序,它使用以下实用程序函数获取给定目录路径中的文件列表

void listTopLevelFiles(const boost::filesystem::path& directoryPath, std::vector<std::string> &topLevelFiles) {
    if (!exists(directoryPath))
        return;
    //non-recursive directory iterator
    boost::filesystem::directory_iterator end_itr;
    for (boost::filesystem::directory_iterator itr(directoryPath); itr != end_itr; ++itr) {
        bool selectEntry = boost::filesystem::is_regular_file(itr->status());
        if (selectEntry && (itr->path().parent_path() == directoryPath)) {
            topLevelFiles.push_back(itr->path().string());
        }
    }
}
void listTopLevelFiles(常量boost::filesystem::path和directoryPath,std::vector和topLevelFiles){
如果(!存在(目录路径))
返回;
//非递归目录迭代器
boost::filesystem::directory\u iterator end\u itr;
for(boost::filesystem::directory\u迭代器itr(directoryPath);itr!=end\u itr;++itr){
bool selectEntry=boost::filesystem::is_regular_file(itr->status());
if(选择entry&(itr->path().parent_path()==directoryPath)){
topLevelFiles.push_back(itr->path().string());
}
}
}
然而,从度量使用情况来看,我发现对于某些用户和某些实例,topLevelFiles向量是空的。在某些情况下,我指的是以下场景

  • 通过获取顶级文件,应用程序可以正常工作
  • (*一段时间后*)重新打开应用程序,向量大小为0(应用程序崩溃)
  • 连续多次重新打开时大小为0(应用程序崩溃)
  • (用户做了一些魔术-重启机器?!)应用程序工作-顶级文件正确填充
不幸的是,我无法在我的windows机器上复制这个。需要注意的一点是,此应用程序的mac用户不会出现此问题

应用程序的上下文:在main.exe所在的文件夹中,有某些数据文件与应用程序捆绑在一起。使用std::ifstream读取这些文件,并将其复制到字符串流

考虑到这个问题是暂时的,并且只发生在Windows中,我怀疑引起问题的Windows中臭名昭著的文件锁定。(应用程序的前一个实例留下了一些打开的文件句柄?)

下面是一些代码片段,这些代码片段与从上述函数获取向量后这些数据文件发生的任何情况有关

bool createFileStream(const std::string& filePathUTF8, std::unique_ptr<std::ifstream>& fileStream) {
    // Windows doesn't accept filenames encoded in UTF8. convert to wide string.
    std::wstring filePathW = StringUtils::getWString(filePathUTF8);
    std::ifstream* file = new std::ifstream(filePathW);
    if(file->is_open()) {
        fileStream.reset(file);
        return true;
    }
    return false;
}

bool decryptFileStream(std::unique_ptr<std::ifstream> inputFileStream, std::unique_ptr<std::istream>& outputIStream) {
    if(inputFileStream == nullptr)
        return false;
    //Get the length of the file stream
    inputFileStream->seekg(0, std::ios::end);
    size_t length = static_cast<size_t>(inputFileStream->tellg());

    inputFileStream->seekg(0, std::ios::beg);
    inputFileStream->clear();
    //Check that the file stream is in good state
    if (!inputFileStream->good())
        return false;
    //Decrypt the file stream into a stringstream
    {
        std::unique_ptr<char[]> buffer;
        buffer.reset(new char[length]);
        //Decrypt the file stream
        int64_t readSize = decryptFileStreamFromOffset(inputFileStream.get(), length, (uint8_t *)buffer.get());
        if(readSize == length) {
            outputIStream.reset(new std::istringstream(std::string(buffer.get(), length)));
            return true;
        }
    }
    return false;
}
bool createFileStream(常量std::string和filePathUTF8,std::unique\u ptr和fileStream){
//Windows不接受UTF8编码的文件名。请转换为宽字符串。
std::wstring filePathW=StringUtils::getWString(filePathUTF8);
std::ifstream*file=新std::ifstream(filePathW);
如果(文件->处于打开状态()){
重置(文件);
返回true;
}
返回false;
}
bool decryptFileStream(std::unique\u ptr inputFileStream、std::unique\u ptr&outputIStream){
if(inputFileStream==nullptr)
返回false;
//获取文件流的长度
inputFileStream->seekg(0,std::ios::end);
size\u t length=static\u cast(inputFileStream->tellg());
inputFileStream->seekg(0,std::ios::beg);
inputFileStream->clear();
//检查文件流是否处于良好状态
如果(!inputFileStream->good())
返回false;
//将文件流解密为stringstream
{
std::唯一的ptr缓冲区;
重置(新字符[长度]);
//解密文件流
int64_t readSize=decryptFileStreamFromOffset(inputFileStream.get(),length,(uint8_t*)buffer.get());
if(readSize==长度){
reset(新的std::istringstream(std::string(buffer.get(),length));
返回true;
}
}
返回false;
}

我是否做了一些错误的事情,这是一种不好的做法,必然会引起问题?我应该如何继续?

只是胡乱猜测-目标计算机上是否安装了任何防病毒解决方案?您是否尝试过禁用Windows Defender?任何防病毒解决方案都必须在100%的时间内导致该问题。但经过某些尝试后,这个问题就消失了。另外,它是一个无害的小json文件。您如何知道向量是空的,以及程序为什么/在哪里崩溃?该计划是如何启动的?运行(工作目录、权限、环境)之间是否可能存在差异?是否可能同时写入文件?当文件长度为01时,程序可能会出现错误\n代码循环通过
topLevelFiles
vector,并将解密的istream存储在另一个
streamVector
中。假设一切顺利,代码会在崩溃的地方访问streamVector[0]。3\是GUI应用程序;用户双击exe;listTopLevelFiles是最先执行的文件之一,因此在启动时就会发生崩溃。4\n在运行之间对环境没有有意更改。5\无代码写入这些文件;它只能从中读取;(用户可以双击同一个exe多次,从而导致并行ifstream只读)6\理想情况下,文件永远不会更改,并且不能为0。>>任何防病毒解决方案都必须在100%的时间内导致问题。不一定。看起来您正在加密/解密数据,一些AV解决方案分析解密的数据(如果您安装了打包的恶意软件怎么办?),然后计算散列,然后将散列与已知的恶意软件散列进行比较(同时锁定文件)。如果散列已经过验证,它将被缓存,因此不会发生锁定