C ++如何读取目录中的所有.txt文件? [英] C++ How Do I Read All .txt Files in a Directory?
问题描述
如何读取特定目录中的所有.txt文件?假设在我的C:\
中有一个foo.txt
和foo2.txt
.有没有一种方法可以阅读这两者,而不必做这样的事情?
How do I read all .txt files in a certain directory? Let's say in my C:\
I have a foo.txt
and a foo2.txt
. Is there a way to read both of these two without having to do something like this?
string text;
string text2;
ifstream myFile ("foo.txt");
ifstream myFile2 ("foo2.txt");
while(myFile << text){
};
while(myFile2 << text2){
};
换句话说,是否可以放置通配符*.txt
来指示读取所有.txt
文件?
In other words, is it possible to put a wildcard *.txt
to instruct to read all .txt
files?
ifstream myFile ("*.txt");
推荐答案
由于我看到您使用的是Windows,因此此处为FindFirstFile
,FindNextFile
.不需要Windows机器,所以我无法举个例子,但是这里有资源:
Since I see you are using windows here is a FindFirstFile
, FindNextFile
. Do not have Windows machine handy so I cannot give you example but here are the resources:
您可以提取ifstream的文件名.
You can than extract file name for ifstream.
或者您可以使用C ++ 17 目录迭代器.我根本没有玩过.
Or you can use C++17 directory iterator. I have not played with this at all.
这篇关于C ++如何读取目录中的所有.txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!