Absolute paths and slashes and backslashes in relative paths

1. Path in C++ file Forward slash and backslash in
file path


Forward slash, also known as left slash, symbol is "/";


backslash, also known as right slash, symbol is "\".


The representation of file path can be divided into absolute path and relative path:


1. Absolute path
Absolute path representation is relatively easy,


for example, to open "C: \Documents andSettings\All Users\My Documents\Downloads\pillow.jpg", then:


Fp = fopen("C: \\Documents andSettings\\All Users\\My Documents\\Downloads


\\pillow.jpg ", "rb");


gives all the paths starting from the drive letter, it should be noted here that "\ "Use the double slash "\\", because the path is enclosed in double quotes, which is equivalent to a string, and the backslash in the string needs to be used in conjunction with a backslash to represent an escape character.


2. Relative path
The default access directory of the vc project is the project directory. The relative path has the following forms. The absolute path of the file in the


current working path (the directory where DSW is located) The writing method of the relative path D: \VS2008\test\test.dsw D: \VS2008\test\ pillow.jpg Fp =fopen ("pillow.jpg ", "rb"); D: \VS2008\test\test.




















D: \VS2008\test\src\ pillow.jpg


Fp =fopen(".\\src\\pillow.jpg", "rb");


or Fp =fopen("src\\pillow.jpg", "rb" );


D: \VS2008\test\test.dsw


D: \VS2008\src\ pillow.jpg


Fp =fopen ("..\\src\\pillow.jpg", "rb");


 


 


Example 1:
Fp =fopen ("pillow.jpg ","rb"); The bmp file is in the vc project directory, and it belongs to the same directory as the dsw file.


Example 2:
Fp =fopen ("..\\src\\pillow.jpg", "rb");


"../" means the parent directory of the current directory, that is, the current parent directory. Indicates that the JPG file is in the same level directory src of the project directory, so the path is to exit the project directory and then enter the src directory to access the JPG file. ".." means to return to the previous directory (parent directory) of the current directory


Example 3:
 Fp =fopen(".\\Downloads\\pillow.jpg", "rb");


".\" add or not add both Same as the current directory. Indicates that the JPG file is in the subdirectory src of the project directory, ".












"/" indicates the start of the root directory.


Current HTML absolute path HTML absolute path


to be linked to


Link path


D:\workspace\TEST_HTML\main.htm


D:\workspace\test \link.html


"../test/link.htm"


D:\workspace\TEST_HTML\ main.htm


D:\workspace\TEST_HTML\test\link.html


"./test/link.htm"


D:\workspace\TEST_HTML\main.htm


D: \test\link.html


"/test/link.htm"


3. Path representation
in Unix/Linux, the path is separated by a forward slash "/", such as "/home/hutaow"; while in Windows, the path is separated by a backslash "\", For example "C:\Windows\System".


4. Path summary
       Sometimes we will see such path writing, "C:\\Windows\\System", that is, use two backslashes to separate the path, which is often seen in network applications or programming. In fact, the above path can be replaced with "C:/Windows/System" without error. But if it is written as "C:


For example: if you want to output a newline, you need to add the "\n" flag; to output a TAB, add "\t"; to output double quotation marks ('"'), you need to input "\"". So what if you want to type a backslash? It's very simple, just type "\\".


  Seeing this, you will understand that if the path string "C:\Windows\System" is compiled by the C compiler, the string actually written to the memory does not contain the backslash "\", but "\W" ", " \S" are escaped into other characters together, and it is bound to cause problems if called again.


 


String parsing is not limited to C compilers, Java compilers, parsing of some configuration files, Web servers, etc., all encounter the problem of parsing strings. Since the traditional Windows adopts the path separation form of a single slash, unnecessary errors may occur when parsing the file path, so the form of separating paths with double backslashes "\\" appears. Regardless of whether the parsing engine parses the backslash into an escape character, the final result is "\" in memory, and the result will not be a problem.


  It can also be seen from this that Windows or DOS was not thoughtful enough in the early design. In order to distinguish it from some features of Unix, the forward slash "/" in Unix was changed to a backslash "\". One problem with this change was that in the early days of the DOS command line, normal filenames could not contain spaces. Look at the following example:


For example, if you want to enter the "hutaowyuan" directory (ignoring the 8.3 naming rules for now),


directly enter "cdhutaow yuan", the command line will parse it into the "hutaow" directory, and the following "yuan" " do parameters, which is clearly not what is expected.   


       In Unix, if the file name contains spaces, it can be escaped directly by adding a backslash "\" before the space, so as to distinguish it well from command parameters (parameters are generally separated by spaces). Still the above example, in Unix, as long as you enter "cdhutaow\ yuan" (add "\" before the space in front of yuan), the command line will correctly identify "hutaow yuan" and enter this directory.


       In the early DOS command line, if it contains spaces, it will cause the command parsing to be unable to distinguish it from the parameters when such a file name is entered. If you enter a backslash, it means the path "hutaow\ yuan". Of course, now later versions of Windows have solved the whitespace problem using other methods (such as enclosing filenames in double quotes).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324459118&siteId=291194637