Validus Oculus
Validus Oculus

Reputation: 2701

What is the correct syntax for portable fstream file paths?

I have seen this annoying question again and again. Could you please share your knowledge that might help us to find the answer.

My confusion is that, forward-slash is posix standard but directory structure of operating systems are different.

Thank you

What is the correct syntax for portable fstream file paths?

(e.g. the string you would pass to std::fstream:open() to open a file.)

A. "::directory:file.bin"

B. "C:/Directory/File.bin"

C. "/directory/file.bin"

D. "C://Directory//File.bin"

E. std:fstream file paths are not portable.

Upvotes: 4

Views: 2590

Answers (4)

user1976
user1976

Reputation: 383

Paths are not portable. It is futile to try to use a portable syntax, since any that you come up with may not be portable between future filesystems. However, it should be possible to do one of the following:

  1. Design your application so that you do not need to store paths.
  2. Use your own path format which you can covert into paths on the current system.
  3. Restrict the paths that your application uses to a portable (between Windows/Linux) subset (e.g: relative paths with "/" as path separator)
  4. Store multiple versions for the same path and use the correct one on each supported OS.
  5. Store one form of paths (e.g. Linux paths) and use a simple scheme to convert to e.g: Windows paths. Document this scheme so that the user understands how paths are interpreted.

I wrote more on this here: https://stackoverflow.com/a/40980510/2345997

Upvotes: 0

Jonathan Mee
Jonathan Mee

Reputation: 38919

What you and many of the rest of us are eagerly awaiting is the FileSystem Technical Specification: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4099.html

It is largely an import of boost/filesystem into the C++ standard.

The technical specification has been made available as part of C++'s experimental section. However, that isn't implemented by default in gcc 4.9.2 or Visual Studio 2013.

Here's to hoping it's coming soon!

You can see more information here: http://en.cppreference.com/w/cpp/experimental/fs

What you are specifically looking for is path: http://en.cppreference.com/w/cpp/experimental/fs/path

By way of answering your problem in the now though if you're looking to do this you need to use #ifdefs and implement your code per target platform.

Upvotes: 4

Martin Ba
Martin Ba

Reputation: 38795

E : not portable, i.e. implementation defined

Answer can be found in the std::fopen docs: (which are referred to by fstream via filebuf::open)

Notes

The format of filename is implementation-defined, and does not necessarily refer to a file (e.g. it may be the console or another device accessible through filesystem API). On platforms that support them, filename may include absolute or relative filesystem path.

Upvotes: 5

Tony Delroy
Tony Delroy

Reputation: 106126

(E) - there is no portable standard, as different filesystems and Operating Systems have different expectations and restrictions. fstreams don't restrict you to the minimum always-supported subset of all actual implementations, or you'd only be able to write "8.3" filenames to the current working directory ;-P

That said, if you're interested in this "problem space", you'll probably want to check out the boost filesystem library, which is not Standard, but is at least widely known....

Upvotes: 1

Related Questions