读取具有相似名称的多个文件c ++ [英] read multiple files with quite similar names c++

查看:170
本文介绍了读取具有相似名称的多个文件c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从当前目录中读取一个文件

I am reading a file from current directory

ifstream myfile;
myfile.open("version1.1.hex");

现在出现的情况是,如果用户更新版本,那么会有 version1.2。十六进制 version1.3.hex ..so,但每次只有一个文件。我想写一个代码,这将满足这种未来需要阅读不同的文件。

Now a situation is arising that if user updates version then there will be version1.2.hex or version1.3.hex ..so on in the current directory, but one file at a time will be present. I want to write a code now which will cater this future need of reading different file.

我在C ++ / CLI中编写此代码。

I'm writing this code in C++/CLI.

推荐答案

因为文件列表有点环境依赖我不知道这是否对你有帮助,
,但这里是一个例子如何实现你的目标在mircosoft体制下。

Since file listings are a bit environment-dependant I am not sure if this is helpful to you, but here is an example how to achieve your goal under the mircosoft regime.

需要的是查询与fileSearchKey匹配的所有文件的FindFirstFile / FindNextFile调用。然后,您可以使用 WIN32_FIND_DATAA 的cFileName部分作为您打开的命令的参数

What is needed is the FindFirstFile / FindNextFile calls which query all files matching the fileSearchKey. Then you can use the cFileName part of WIN32_FIND_DATAA as parameter to your open command

string fileSearchKey = "version*";

WIN32_FIND_DATAA fd;

bool bFirstRun = true;
bool bFinishedRun = false;
HANDLE h = INVALID_HANDLE_VALUE;
while (!bFinishedRun)
{
    if (bFirstRun)
    {
        h = FindFirstFileA(fileSearchKey.c_str(), &fd); 
        bFirstRun = false;
    } else
    {
        if (FindNextFileA(h, &fd) != FALSE) 
        {
            // Abort with error because it has more than one file or decide for the most recent version
        } else
        {
            bFinishedRun = true;
        }
    }

}
// Load file
ifstream myfile;
myfile.open(fd.cFileName);

这篇关于读取具有相似名称的多个文件c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆