0

I have an assignment that require me to develop a program that is passed either a file name or a directory name.

if what is passed is a directory name then i have to read all the files in that directory and basically do some processing on them.

the problem is that I am using c++ and c++ out of the box doesn't really play nice with file systems without third party/system libs.

how can I read all the files from a directory without platform dependency because I am using a windows machine and the grader is using linux machine.

I tried to use filesystem and I have gcc 9.2 but it doesn't compile

it doesn't compile this file:

#include <iostream> 
#include <filesystem>

namespace fs = std::filesystem;

int main(){
    
    fs::path p("some-path");
 
}

it results in this compiler error:

testfs.cpp:4:21: error: 'filesystem' is not a namespace-name
    4 | namespace fs = std::filesystem;
      |                     ^~~~~~~~~~
testfs.cpp: In function 'int main()':
testfs.cpp:8:5: error: 'fs' has not been declared
    8 |     fs::path p("some-path");
   
Xenouth
  • 5
  • 4

1 Answers1

1

filesystem::path is by far the best cross-platform solution for platforms with a compiler that supports C++17, the first to support filesystem, or a newer Standard revision.

GCC 9.2 doesn't default to compiling to the c++17 standard revision.

Force C++ 17 support by adding -std=c++17 to the command line, selecting the C++17 dialect in your IDE's Compiler option property pages, or editing the appropriate configuration file to add the option.

Examples with and without the extra compiler option: https://godbolt.org/z/Tj6car

If a C++17 or newer compiler is not available on your system, consider using boost::filesystem

user4581301
  • 33,082
  • 7
  • 33
  • 54