When one uses ifstream::open...

This is a discussion on When one uses ifstream::open... within the C++ Programming forums, part of the General Programming Boards category; When one uses ifstream: pen(), what is the correct format for filepaths? And is there a standard for this that ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question When one uses ifstream::open...

    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?

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    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

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    In Windows, would one pass a filepath like this:
    C:/Program Files/Program/Program.cpp
    or like this:
    C:\Program Files\Program\Program.cpp

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Generally like this. For safety reasons.

    Code:
    C:\\Program Files\\Program\\Program.cpp

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Brafil View Post
    Generally like this. For safety reasons.

    Code:
    C:\\Program Files\\Program\\Program.cpp
    Ok, thanks.

    Would this also work:

    Code:
    C://Program Files//Program//Program.cpp

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    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++.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    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.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    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?
    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

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    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.
    Thanks. That is what I guessed was the case, so I went ahead and adjusted my code to accept only forward slashs in the filepath.
    I prefer passing in single forward slashs instead of double backward slashs, because I think it looks cleaner that way.
    Last edited by Programmer_P; 05-30-2010 at 01:22 PM.

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Note that you do not have to call open if you pass the filepath into the constructor of the file stream object.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream::open ??
    By Sicilian in forum C++ Programming
    Replies: 1
    Last Post: 01-30-2002, 11:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21