从C ++中的给定文件点读入目录 [英] Read in a directory from a given file point in C++

查看:120
本文介绍了从C ++中的给定文件点读入目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个程序,将同时读取/写入文件到同一目录(但不是同一个确切的文件在同一时间)。我有写部分完成,但我正在努力得到一半的体面和工作实现的阅读目录部分。



目录中的文件遵循以下命名方案:
Image- [INDEX] - [KEY / DEL] TIMESTAMP]



[INDEX] 000000 , [KEY / DEL] 根据图片是键还是增量框交替显示, [TIMESTAMP] / code>是文件创建时的Unix / Linux时期。



现在,阅读程序读入目录(使用 dirent.h 库)时间每次它需要在目录中找到一个图像。当目录变得非常大时,我想象这个操作/方法将迅速变得非常资源密集,并最终失败。所以,我试图找到一种替代方法。我想在初始化时在整个目录中读取,并将文件信息保存在数组中以便稍后在程序中访问/使用。然后,当请求一个不在数组中的文件时,程序将通过读入目录来更新文件数组,但是这个时间从在初始化结束时它离开的点开始。



这是否可能?要开始读取目录中已知点(最后一个文件读入)中的文件名中的文件名?或者我必须从头开始每次都开始?



还是有更好的方法吗?



谢谢。


>如果你可以折扣文件被创建的顺序的可能性,也就是说,没有文件
你希望在该文件之前创建另一个文件之前处理,那么你可以使用这个方法。



首先,将整个目录列表读入数组或向量。然后,当迭代文件时,只是迭代向量。最后,如果你得到一个文件未找到或到达向量的结尾,刷新它,以防万一已创建。



你将无疑封装这个逻辑转换成某种上下文对象,它记住最后一次读取的文件。您也可以通过排序向量进行优化。


I have two programs that will be reading / writing files to the same directory at the same time (but not to the same exact files at the same time). I have the writing portion done, but I am struggling to get a half way decent and working implementation of the reading directory portion.

The files within the directory follow the following naming scheme: Image-[INDEX]-[KEY/DEL]--[TIMESTAMP]

[INDEX] increments up from 000000, [KEY/DEL] alternates based on whether the image is a key or a delta frame and [TIMESTAMP] is the Unix / Linux epoch time at file creation.

Right now, the reading program reads in the directory (using the dirent.h library) one file at a time every time it needs to find an image within the directory. When the directory gets extremely large, I would imagine that this operation / method will quickly become extremely resource intensive, and eventually fail. So, I am trying to find an alternative method. I was thinking of reading in the entire directory at initialization, and saving the file information in an array to access / use later in the program. Then, when a file is requested that is not in the array, the program would go and update the array of files by reading in the directory, but this time starting from the point it left off at the end of the initialization.

Is this possible? To start reading in the file names within a directory at a known point (the last file "read in") in the directory? Or do I have to start all the way from the beginning each time?

Or is there a better way of doing this?

Thanks.

解决方案

As Andrew said, I would confirm that this is actually a problem before trying to solve it.

If you can discount the possibility of files being created out of sequence, that is, no file you wish to process before another file will ever be created after that file, then you can use this method.

First, read the entire directory listing into an array or vector. Then, when iterating files, just iterate the vector. Finally, if you get a file not found or reach the end of the vector, refresh it just in case more have been created.

You will no doubt want to encapsulate this logic into some sort of context object, which remembers the last file read. You could also optimise by sorting the vector.

这篇关于从C ++中的给定文件点读入目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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