searching .txt files from folders(how can i do this?)

i want to get a folder path from user then the program will find the .txt files in this folder. how can i do this?



Hello okancnplt,

Not that I would advise allowing the user to enter a path because there is much room for error. I would use "std::getline()" for input of the path.

I find it a better choice to set the path in the program and just the user deal with a file name. Much less problem this way.

It is your program, so you can set it up anyway you like.

Hope that helps,

Andy
Here is a simple demo how to find all files in a directory.
Needs C++17 compiler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <experimental/filesystem>

using namespace std;
using namespace std::experimental::filesystem;

int main()
{
  string folder;
  cout << "Enter folder: ";
  getline(cin, folder);

  using iterator = directory_iterator;
  for (iterator iter(folder); iter != iterator{}; ++iter)
  {
    cout << iter->path().string() << "\n";
   // TODO check if filename ends with .txt
  }  
}

Topic archived. No new replies allowed.