When one uses ifstream:pen(), what is the correct format for filepaths?
And is there a standard for this that will work across all major OSes?
When one uses ifstream:pen(), what is the correct format for filepaths?
And is there a standard for this that will work across all major OSes?
No, it is implementation-specific. Forward slash works everywhere I've seen.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
In Windows, would one pass a filepath like this:
or like this:C:/Program Files/Program/Program.cpp
C:\Program Files\Program\Program.cpp
Generally like this. For safety reasons.
Code:C:\\Program Files\\Program\\Program.cpp
No. Reason is '\\' is an escaped character, as is '\/' , '\n' , \<anything>. For reasons that I hope are obvious, because \ is the escape-character, you have to escape backslash as well.
What you typed is two forward slashes.
FYI: Whether an escaped character has meaning apart from its un-escaped counterpart is implementation-defined in C and C++.
So if I understand you right, you're saying I can't use '//' in place of '\\' because '\\' is an escaped character while '//' isn't?
What if I use just one forward slash between directories in filepaths?
That way, I end up with a forward slash regardless of what OS the program is run on. Then I can just check the first character of the file path, to see if it starts with a drive letter or not (like on Windows). If so, I'll scan through the rest of the filepath, checking to make sure no unsupported characters are in the filepath. And if not, and it starts with a / instead, I'll assume the program is being run on a Linux OS, and similarly, I'll scan it for any unsupported characters first before passing it to ifstream:pen, to attempt to open the file.
The point about escaping is that in C/C++ and most other places the escape character is the backslash, \. So \n escapes the n to mean newline, \\ indicates a real backslash (otherwise there will be obvious problems, eg "\here\now").
So plain old / is fine, and WRT to filesytems it will work on *nix for sure. Why MS made the choice to use / instead long ago with DOS I dunno, as that was already the situation.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Last edited by Programmer_P; 05-30-2010 at 01:22 PM.
Note that you do not have to call open if you pass the filepath into the constructor of the file stream object.