In the file path, the difference between slashes and backslashes between windows and linux systems

1. Representation of the file path

The windows system and the linux system use backslashes and slashes to separate paths. It is easy to confuse people who use the two operating systems.

  • The windows system uses a backslash "\" to separate the path, for example F:\debugTest\test\data
  • The linux system uses the slash "/" to split the path, such as /opt2/test/subdir

To help memory, in windows system, one of the first letter W is drawn as \, so use a backslash.

In addition, double slashes or double backslashes can ask for shared computer files. The specific format is:

Windows system: \\computer name or ip\folder\file name

Linux system: //computer name or ip/folder/file name

2. Representation of the path during programming

When programming, when using a string to represent a path, if there is \ in the path, you need to pay special attention. Because in the string, \ represents a shift character, "\" together with the first character behind represents a character, such as \n represents a newline.

Therefore, if it is a windows system, double backslashes should be used to indicate path division in programming. For example, to open a path:

ofstream outfile("F:\\debugTest\\test\\data\\f1.txt", ios::out);

The path inside must be represented by double backslashes, otherwise an error will occur.

Guess you like

Origin blog.csdn.net/weixin_43354152/article/details/130277312