Get the App
SLTechnology News&Howtos  ›  Servers  › 

The windows directory\ is used / replaced in actual development

Shulou Source: shulou.com Published: 2022-06-02 07:23:44 09月27日 Update

# include try not to use\, but use /, so as to migrate the code to the Linux environment

The code is as follows:

# include "stdafx.h"

# include

# include

# include

Using namespace std

Int _ tmain (int argc, _ TCHAR* argv [])

{

Char utfBuffer [256] = {0}

Ifstream utffile ("E:/doc/Android/Makefile"); / / OK

/ / ifstream utffile ("E:\ doc\ Android\ Makefile"); / / ERROR

/ / ifstream utffile ("E:\ doc\\ Android\\ Makefile"); / / OK

Utffile.getline (utfBuffer, 100)

Size_t inLen = strlen (utfBuffer)

Return 0

}

Summary:

The first scheme uses the same slash as the Unix system, which is convenient to transplant to the Linux platform in the future development. after all, it is more troublesome to modify the slash in the header file include.

The third method uses a double backslash, which tells the compiler that the preceding backslash is not used to mask escape, but is real.

The second error scheme is analyzed in detail:

Split the string:

Char utfBuffer [256] = {0}

Char szPathName [256] = {"E:\ doc\ Android\ Makefile"}

Ifstream utffile (szPathName)

Utffile.getline (utfBuffer, 100)

Size_t inLen = strlen (utfBuffer)

Return 0

By single-step debugging the actual content of szPathName becomes: the "E:docAndroidMakefile" compiler thinks that\ is used to prohibit escape, but does not expect that the user is actually used as a backslash, so it is cleared, resulting in a failure to read the file path.

Tags: Slash actual code file scheme compiler but escape migration compilation development content characters strings actually platform methods aspects appearance environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei Docker MariaDB Shulou Tech Info