-3

I am trying to open all text files in the same folder as my main.cpp. How do I go about doing it?

file.open(".txt");

I tried doing this, I tried using *, tried using modulo, but none of it works. Help is appreciated, thank you.

Other question is asking how to get a list of files. I'm asking how to open them. I don't want to list them.

belgarion
  • 115
  • 1
  • 11

2 Answers2

1

There is no standard interface for listing files in a directory or matching them against a glob in C++.

You have to depend on a platform specific API to achieve this. To figure out which one, you have to first figure out what system you are programming for.


The upcoming C++17 standard will have an API for listing files in a directory, and it already exists as an optional TS. There is no globbing functionality however. As an alternative, wild card matching can be implemented with regular expressions.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

I don't think something like that is possible in cpp. Each file object is opens a single file with the path specified. If you want to open all the files in the folder I'd suggest you list the files in the directory and iteratively open all the required files.

the std::filesystem::directory_iterator(path) returns an iterator that you can use to select the respective file and then open them

morfindel
  • 132
  • 9
  • Once I have the list of text files, how would I, for example, grab the name of the first file to read it? – belgarion May 26 '17 at 19:21
  • Yup. You would iterate through all the files and then read them as you have mentioned above using `file.open()` – morfindel May 27 '17 at 12:00