Reputation: 2701
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
(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
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:
I wrote more on this here: https://stackoverflow.com/a/40980510/2345997
Upvotes: 0
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 #ifdef
s and implement your code per target platform.
Upvotes: 4
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
Reputation: 106126
(E) - there is no portable standard, as different filesystems and Operating Systems have different expectations and restrictions. fstream
s 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