1

I would like to access a directory, and parse all the file in it. To do so, I found a way using on this post, but apparently it is for C, not C++ (tell me if I am wrong).

I am using Qt, and I would like to know which similar way to use to run through each file of my directory.

Community
  • 1
  • 1
Tofuw
  • 908
  • 5
  • 16
  • 35
  • 2
    What about `QDir::entryInfoList()`? – vahancho Feb 23 '16 at 10:46
  • 1
    As an aside, most C libraries can be used within a C++ program. They are different languages, and C is not quite a subset of C++, but 9 times out of 10 (or probably more), you can use C libraries within C++. It can be a bit ugly though, but you can wrap it up nicely. – BoBTFish Feb 23 '16 at 10:48
  • @vahancho I didn't though about it ! I'll use it right away ! @BoBTFish So there is a way to use `` in my C++ application using a wrap ? I am not at ease with C++ so I may says wrong things and misunderstanding. – Tofuw Feb 23 '16 at 10:58
  • 2
    @Tofuw, yes just remeber to add extern "C" so it would be: extern "C" { #include } – Marek Waszkiewicz Feb 23 '16 at 11:43

2 Answers2

5

Have you looked at the QDir documentation ? There's all you need here and probably a lot more. Why bother with a C lib ?

Ilya
  • 5,377
  • 2
  • 18
  • 33
  • Since the recommandation of @valancho yes, but i guess it is also a good exercice to me, trying to wrap C library. I'm just a newbie in C++ so if I can explore different way, I'm all for it. – Tofuw Feb 23 '16 at 11:52
  • @Tofuw OK but reading the doc is a good exercise, too. It's a valuable skill you'll need a lot more often than wrapping C libraries (the most useful already have wrappings). – Ilya Feb 23 '16 at 12:02
  • 1
    @Tofuw "it is also a good exercice to me, trying to wrap C library" I disagree. Reinventing the wheel is rarely a good exercise. Go design your car (your intended application), instead of redesigning the wheels. Only if you have got a very nice car that could somehow benefit from special wheels (low level implementation details) will you want to consider redesigning the wheel. Focus on what you want your project to do, and leverage Qt to do it. Since you're a novice, that alone will take some good time, without worrying about implementation details. – Kuba hasn't forgotten Monica Feb 23 '16 at 16:08
  • @Ilya and KubaOber I'll follow your advice so. And it is true that I didn't have the reflex to consult documentation. – Tofuw Feb 23 '16 at 16:24
1

It seems the equivilent to glob is the entryList function in the QDir class. For example.

QStringList pattern = {"calibration-\?\?\?\?-\?\?-\?\?-\?\?\?\?\?\?.zip"};
QDir dir(".");
QStringList files = dir.entryList(pattern);
for (QString file : files) {
  std::cout << file.toStdString() << std::endl;
} 
plugwash
  • 9,724
  • 2
  • 38
  • 51