Differences between relative paths and absolute paths in C ++ and between slashes and backslashes

Source: Internet
Author: User

File Path
Forward and backward slashes

A forward slash, also known as a left slash, indicates "/"; a backslash, also known as a right slash, indicating "\". The 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 ");
All paths starting with the drive letter are provided. Note that "\" must be used with a double slash "\". The default directory accessed by the VC project is the project directory, the relative path has the following forms: pdummyfile = fopen ("dummy.bmp ",
"Rb"). The BMP file is under the VC project directory, which is the same as the DSW file.
Pdummyfile = fopen (".. \ texture \ dummy.bmp "," rb "); indicates that the BMP file is in texture, therefore, the path is to exit the project directory and then access the BMP file in the texture directory. ".." Indicates to return to the upper-level directory (parent directory) of the current directory)
Pdummyfile = fopen (". \ texture \ dummy.bmp", "rb ");
The BMP file is in the texture subdirectory of the project directory, and "." indicates the current default directory, that is, the VC project directory. Then, access the file in the texture subdirectory of the project directory.

Note: For relative paths, "\" in the path representation must also use a double slash "\\",

In C ++, \ is an escape character, which represents a \, just as \ n represents a 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"); or

Cstringfilename = _ T ("D:/matcom45/doc/users/_ themes/M. dat ");
"./"
Add or not both are the same, that is, the current directory

"../"
Indicates the parent directory of the current directory, that is, the parent directory.

 

In Unix/Linux, the path separation adopts the forward slash "/", for example, "/home/huaow". In Windows, the path separation adopts the Backslash "\", for example, "C: \ WINDOWS \ SYSTEM ".

Sometimes we can see the path format "C: \ Windows \ System", that is, using two backslash to separate the paths, this method is often seen in network applications or programming. In fact, the above path can be replaced by "C:/Windows/system" without errors. However, if you write "C: \ WINDOWS \ SYSTEM", there may be various strange errors. The cause of the above problem should be analyzed from the aspect of string parsing. Anyone who has learned programming should know that when outputting a string in C, If You Want To output a line feed, add the '\ n' sign. Similarly, output a tab, '\ T' is added, that is, the backslash ("\") is used to combine the characters following it and convert them to other characters. According to this principle, if you want to output double quotation marks ('"'), you need to enter '\"' to correctly write the string containing double quotation marks into the memory. What if you want to enter a backslash? It's easy. Just press.

Some people may have noticed that, if the path string "C: \ WINDOWS \ SYSTEM" is handed to the C compiler for compilation, the string actually written into the memory does not contain the Backslash "\", and even the letters that follow the backslash are escaped into other characters. If you call it again, the problem may occur.

String Parsing is not limited to the C compiler, Java compiler, parsing of some configuration files, Web servers, and so on, and will all encounter this problem of string parsing, traditional Windows uses the path separation form of a single slash, which may cause unnecessary errors when parsing the file path, therefore, the form of separating paths with double Backslash "\" is displayed. No matter whether the parsing engine parses the backslash into an escape character or not, the final result obtained in the memory is "\", and no problem will occur.

From this we can also see that windows or DoS is not fully considered at the initial stage of design, in order to distinguish it from some characteristics of UNIX, change the path Separation Method of the forward slash "/" in UNIX to the Backslash "\". In the early days of the doscommand line, a normal file name cannot contain spaces. If a file name contains spaces, command Parsing cannot distinguish it from parameter. For example
Yuan "this directory (ignore the 8.3 naming rule first), enter" CD huaow yuan "directly, and the command line will parse it into the" huaow "directory, the "Yuan" parameter is not expected.

In UNIX, if a file name contains spaces, you can directly add a backslash "\" before the space to escape the file name, thus, it is very different from the command parameters (parameters are generally separated by spaces ). In the above example, in UNIX, you only need to input "CD
The command line correctly identifies "huaow yuan" and enters the directory by adding "\" to the space in front of Yuan. Of course, other methods (for example, the file name is enclosed in double quotation marks) have been used in subsequent versions of Windows 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.