Relative path and absolute path file path in C + + __c++

Source: Internet
Author: User
Tags parent directory
Relative and absolute paths in C + + and the difference between a slash and a backslash
File path
Forward slash and back slash

Forward Slash, also known as the left slash, the symbol is "/", the backslash is also called the right slash, the symbol is "\". The representation of a file path can be divided into absolute and relative paths: 1, absolute path representation is relatively easy, for example

Pdummyfile =fopen ("D:\\vctest\\gltexture\\texture\\dummy.bmp", "RB");
Given the start of the entire path from the letter, you need to pay attention to the "\" to use double slash "\", VC project access to the directory is the project directory, the relative path has the following various forms, such as: Pdummyfile = fopen ("Dummy.bmp", "RB"); BMP file is in the VC project directory, and DSW files belong to a directory.
Pdummyfile = fopen (".. \\texture\\dummy.bmp "," RB "); Represents BMP files in the engineering directory of the same sibling directory texture, so the path is to exit the project directory and then enter the texture directory access to BMP files. “..” Indicates the level of the directory (parent directory) that is retired to the current directory
Pdummyfile =fopen (". \\texture\\dummy.bmp", "RB"); Indicates that the BMP file is in the subdirectory texture of the engineering directory, "." Represents the current default directory, the VC project directory, and then access to the file in its subdirectory texture
Note that for relative paths, the "\" in the path representation also uses double slash "\".
C + + is an escape character, he represents a \, just like \ n means carriage return.
So the path name in C + +:
D:\matcom45\doc\users\_themes\m.dat
should be:
CString filename=_t ("D:\\matcom45\\doc\\users\\_themes\\m.dat");
cstringfilename=_t ("D:/matcom45/doc/users/_themes/m.dat");
"./"
Plus not all the same, refers to the current directory
".. /"
Represents the ancestor directory of the current directory, which is the current parent directory.
In Unix/linux, the path is delimited by a forward slash "/", such as "/home/hutaow", whereas in Windows, the path is delimited with a backslash "\", such as "C:\Windows\System".
Sometimes we will see this path to write, "C:\\Windows\\System", that is, with two backslashes to separate the path, which is often seen in Web applications or programming, in fact, the above path can be replaced with "C:/windows/system", no error. But if you write "C:\Windows\System," there may be a variety of strange mistakes. The reasons for the above problems should be analyzed from the aspect of string parsing. Anyone who has learned to program should know that in C, when the output string is if you want to output a newline, add ' \ n ' This flag, like, output a tab, plus ' \ t ', that is, the backslash ("\") This symbol will be followed by the character of the combination to escape to other characters. According to this principle, if you want to output double quotes (' "), you need to enter ' \ ', so that the string containing the double quotes is written in memory correctly. So if you want to enter a backslash. It's simple, just knock ' \ ' on it.
See here maybe some people have seen the point, if "C:\Windows\System" this path string to the C compiler to compile, the actual write memory string does not contain the backslash "\", even the letter immediately after the backslash is also escaped with the other characters, It's bound to go wrong if you call again.
String parsing is not limited to the C compiler, Java compilers, some configuration file parsing, Web servers, and so on, you will encounter the problem of parsing the string, because traditional Windows uses a single slash-delimited form of the path, Causes unnecessary errors when parsing the file path, so there is a way to separate the paths with a double backslash "\". Regardless of whether the parsing engine resolves the backslash to an escape character, the end result in memory is "\" and the results are not problematic.
It can also be seen that Windows or DOS in the early stages of design considerations, in order to distinguish with UNIX features, the forward slash in Unix "/" separated path to the backslash "\". One problem with this change is that in the early DOS command line, a normal file name cannot contain spaces, and if a space is included, the command resolution cannot distinguish it from the parameter area if the file name is entered. For example, to enter the Hutaow
Yuan "This directory (first ignores the 8.3 naming rules), directly enter" CD Hutaow yuan ", the command line resolves it to enter the" Hutaow "directory, and the following" Yuan "to do parameters, which is obviously not expected.
In Unix, the file name, if it contains spaces, can be escaped directly with the backslash "\" in front of the space, which distinguishes it from the command parameters (usually separated by spaces). Or the example above, in Unix, as long as you type "CD hutaow\ Yuan" (in front of the yuan before the space plus "\"), the command line to correctly identify the "Hutaow
Yuan "and enter this directory. Of course, the next version of Windows is now using other methods, such as the file name enclosed in double quotes, to solve the space problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.