5

I've this simple code:

FILE *fIn;
fIn = fopen("c:\\myFiles\\andrea.txt","r");

My problem is: if 'myFiles' folder doesn't exist, there is a way to create it directly from my C++ code ? And if I have more than one directory in my path, how can I create all of them ?

  • 1
    If you don't care about your program being not multi platform, you can use `system("any system command from your shell here")` – MivVG Mar 27 '18 at 13:23
  • What OS are you using? Windows? If so, related/possible duplicate: https://stackoverflow.com/questions/9235679/create-a-directory-if-it-doesnt-exist – Algirdas Preidžius Mar 27 '18 at 13:24
  • @Borgleader thanks but the file isn't my problem –  Mar 27 '18 at 13:25
  • @AlgirdasPreidžius I'm on Unix Ubuntu –  Mar 27 '18 at 13:25
  • 1
    Filesystem operations are operating system-dependent. If you are programming on Windows, use the CreateDirectory call from the Windows API: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx. On Linux/Unix, use the mkdir call from the standard C library. – FBergo Mar 27 '18 at 13:25
  • 4
    Before C++17 and [](http://en.cppreference.com/w/cpp/experimental/fs) there's no platform agnostic way to do that. Luckily, with C++17 you now have [create_directory](http://en.cppreference.com/w/cpp/experimental/fs/create_directory). – Jesper Juhl Mar 27 '18 at 13:25
  • 1
    Since C++17, [`std::filesystem::create_directories`](http://en.cppreference.com/w/cpp/filesystem/create_directory) – Jarod42 Mar 27 '18 at 13:26
  • @FBergo thanks for your solution –  Mar 27 '18 at 13:27
  • 3
    Prior to `C++17` there was always [boost::filesystem](http://www.boost.org/doc/libs/1_66_0/libs/filesystem/doc/index.htm). In particular [create_directories](http://www.boost.org/doc/libs/1_66_0/libs/filesystem/doc/reference.html#create_directories). – G.M. Mar 27 '18 at 13:31
  • @Jarod42 Is it cross platform ? –  Mar 27 '18 at 13:32
  • std::filesystem is cross-platform, but apparently not supported yet in any production compiler: http://en.cppreference.com/w/cpp/compiler_support (only supported in the unreleased development version of GCC, not implemented in MSVC or clang yet). – FBergo Mar 27 '18 at 13:35
  • Possible duplicate of [Cross Platform Way to make a directory?](https://stackoverflow.com/questions/20358455/cross-platform-way-to-make-a-directory) – default Mar 27 '18 at 13:45

4 Answers4

5

In C++17 you have the filesystem header which provides create_directory. That lets you solve your problem.

If you don't have access to C++17, you have to rely on platform specific APIs.

Dominic Hofer
  • 5,751
  • 4
  • 18
  • 23
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • 1
    Is it cross platform ? –  Mar 27 '18 at 13:32
  • 3
    @A. Ortogni It's part of the standard library, so cross-platform is somewhat implied... – Jesper Juhl Mar 27 '18 at 13:33
  • 2
    It is in the [brand new] standard. Whether it is available on your platform depends what toolchains are available on your platform and what standard features they provide. Personally I won't be targetting a platform supporting C++17 for many, many years - you may or may not be in the same boat. – Lightness Races in Orbit Mar 27 '18 at 13:34
  • Thanks for your answers –  Mar 27 '18 at 13:35
  • 1
    std::filesystem is cross-platform, but apparently not supported yet in any stable compiler: http://en.cppreference.com/w/cpp/compiler_support (only supported in the unreleased development version of GCC, not implemented in MSVC or clang yet). – FBergo Mar 27 '18 at 13:37
  • @FBergo Time will fix that. – Jesper Juhl Mar 27 '18 at 13:38
-1

In c++, if the file does not exist it will automatically create a new one using the name you entered. Ofstream file1("myName.txt"); This will automatically create a file named "myName".

-3

ifstream inFile;

inFile.open("c:\myFiles\andrea.txt","r");

  • Please expound on what this answer does instead of just posting code. Also you should be putting 4 spaces in front of your code to format it as code – HeedfulCrayon Mar 27 '18 at 17:09
-4

include

ifstream inFile;

inFile.open("c:\myFiles\andrea.txt","r");

for declaration file

Community
  • 1
  • 1