path settup method
When we set the path on source code
sometimes we use "\\"
or "\"
and sometimes we use 开发者_JAVA技巧"/"
..
What's the environment dependency between them?
The slash ("/") is the standard way to separate components in a filesystem path as per IEEE Std. 1003.1 ("POSIX") and per the Single UNIX Specification. The slash is also the separator character used in URLs. The backslash ("\") is a Windows-specific anomaly. Many programming languages such as Java and Python provide a way for constructing paths in a platform-independent manner. For example, Python provides os.path.join, while Java provides a File.separatorChar constant. In C++, if you use boost::filesystem::path, it overloads operator/ so that it will construct paths using the appropriate path separator. Also, AFAIK, using the standard slash ("/") in the constructor for std::fstream, std::ofstream, std::ifstream works on Windows both with Visual C++ and with GCC / MinGW implementations, so using slashes will probably work in most cases.
As for "\\" vs. "\", if you read it from a file, then all you need is "\". If you place it in a string literal, since "\" is an "escape" character (e.g. "\n") and the interpretation of the escape sequence depends on the character after the first slash, so you need to supply an additional backslash in the literal string for it to be interpreted as a backslash, which is, IMHO, yet another reason to prefer using a regular slash or, better yet, to not hard-code paths as string literals and, instead, to take paths from the user input or from a configuration file.
The path separator on Unix
like operating systems is /
while it is \
on Windows
.
Also since \
is a special char used for creating escape sequences like \n \t \b
, we need to escape the \
itself like \\
to mean a literal \
Since this is tagged as C and C++, you can use a forward slash on most current systems. Windows will also accept a backslash (like it requires at a cmd.exe command prompt) but contrary to popular belief, the internal functions accept forward slashes just fine. Other older systems (e.g., TOPS20, VMS, Classic MacOS) used other separators (dashes, colons, etc.) but among current systems, most of that conflict has disappeared.
/ is the Unix way of doing things
\ is the Windows way of doing things but secretly it will accept / as well so the easiest way to make your code run on Windows and Unix/Linux is to use /
Unix based systems use forward slashes (/
) and Windows uses backslashes (\
) for paths - since a single backslash is used for escaped characters, you need to put it as \\
in the code.
精彩评论