首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

转义字符唤起的疑惑

2013-10-25 
转义字符引起的疑惑\\ 表示 单反斜杠那么我们在拼接字符串的时候,如何表示反斜杠呢? \\test.xml ,是这

转义字符引起的疑惑
'\\' 表示 单反斜杠

那么我们在拼接字符串的时候,如何表示反斜杠呢? "\\test.xml" ,是这样嘛?  还是"\test.xml'

如果是"\\test.xml" 的话, 再看一个例子:

url里的链接拼接,只能用 "\test.html" ,不能使用"\\test.html" 。怎么回事?我已经晕了



[解决办法]

引用:
Quote: 引用:

url里的链接拼接,只能用 "\test.html" ,不能使用"\\test.html"

提问的艺术 去看一遍 
弄懂怎么想别人求助 

什么只能 什么不能 描述清楚
错误提示 运行截图什么的 


引用:
C"\\test.html"=== "\test.html"


首先说 单反斜杠怎么表示, c/c++中是用'\\'表示, 如果是在字符串中,是用"\"表示。

这是基础。

我们结合windows平台 看具体的问题:
对于windows的路径里,有很多反斜杠,所以,我们可以这样使用:"c:\\test.html",这个字符串翻译过来,就是c:\test.html

,再结合网络知识url的表示法这个具体问题file:///c:/abc/def.html,如何用c/c++表示它呢 ?

按照c/c++的只是,一个斜杠是'//',那么file:///c:/abc/def.html用c/c++的表示应该为:

file://////c://abc//def.html 才对, 请注意有几个斜杠。 

这是我根据基础知识分析的结果。

然后编码,结果根本无法正确的访问这个url.所以我才发帖问


谁告诉你/要用'//'来表示的?/就是/,不需要转义
\和/是不一样
[解决办法]
_makepath, _wmakepath
Create a path name from components.

void _makepath( char *path, const char *drive, const char *dir, const char *fname, const char *ext );

void _wmakepath( wchar_t *path, const wchar_t *drive, const wchar_t *dir, const wchar_t *fname, const wchar_t *ext );

Routine Required Header Compatibility 
_makepath <stdlib.h> Win 95, Win NT 
_wmakepath <stdlib.h> or <wchar.h> Win 95, Win NT 


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, retail version 


Return Value

None

Parameters

path

Full path buffer

drive

Drive letter

dir

Directory path

fname

Filename

ext

File extension

Remarks

The _makepath function creates a single path and stores it in path. The path may include a drive letter, directory path, filename, and filename extension. _wmakepath is a wide-character version of _makepath; the arguments to _wmakepath are wide-character strings. _wmakepath and _makepath behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
_tmakepath _makepath _makepath _wmakepath 


The following arguments point to buffers containing the path elements:

drive

Contains a letter (A, B, and so on) corresponding to the desired drive and an optional trailing colon. _makepath inserts the colon automatically in the composite path if it is missing. If drive is a null character or an empty string, no drive letter and colon appear in the composite path string.

dir

Contains the path of directories, not including the drive designator or the actual filename. The trailing slash is optional, and either a forward slash (/) or a backslash (\) or both may be used in a single dir argument. If a trailing slash (/ or \) is not specified, it is inserted automatically. If dir is a null character or an empty string, no slash is inserted in the composite path string.

fname

Contains the base filename without any extensions. If fname is NULL or points to an empty string, no filename is inserted in the composite path string.



ext

Contains the actual filename extension, with or without a leading period (.). _makepath inserts the period automatically if it does not appear in ext. If ext is a null character or an empty string, no period is inserted in the composite path string.

The path argument must point to an empty buffer large enough to hold the complete path. Although there are no size limits on any of the fields that constitute path, the composite path must be no larger than the _MAX_PATH constant, defined in STDLIB.H. _MAX_PATH may be larger than the current operating-system version will handle.

Example

/* MAKEPATH.C */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char path_buffer[_MAX_PATH];
   char drive[_MAX_DRIVE];
   char dir[_MAX_DIR];
   char fname[_MAX_FNAME];
   char ext[_MAX_EXT];

   _makepath( path_buffer, "c", "\\sample\\crt\", "makepath", "c" );
   printf( "Path created with _makepath: %s\n\n", path_buffer );
   _splitpath( path_buffer, drive, dir, fname, ext );
   printf( "Path extracted with _splitpath:\n" );
   printf( "  Drive: %s\n", drive );
   printf( "  Dir: %s\n", dir );
   printf( "  Filename: %s\n", fname );
   printf( "  Ext: %s\n", ext );
}


Output

Path created with _makepath: c:\sample\crt\makepath.c

Path extracted with _splitpath:
  Drive: c:
  Dir: \sample\crt\
  Filename: makepath
  Ext: .c


File Handling Routines

See Also   _fullpath, _splitpath

[解决办法]

转义字符唤起的疑惑转义字符唤起的疑惑
[解决办法]
file:///c:/test/Debug/和 /test.html拼接在一起当然是错误的
或者路径的后面带/,或者 文件名前面加/,二者只能有一处/
否则成了两个/并列了
另外 / 和 \ 并不相同 
虽然,Windows 二者通用,但是C语言中\是转义字符,/不是。
C常量串中, 要连用两个 \ 即\\表示 一个\
C另外除了常量串外,不要在字符里面用双\\代替  \。

用于生成C,C中++ 源代码的,字符串的例外--在代码生成技术中,要再补上一个\。

VC 的源代码中,有时会出现 #include "\\path\\some.h" 这种用法
这是不足为训的。

这里"\\path\\some.h" 
在预编译#include语句中 ,不必写成C串常量一样的格式。
写成#include "\path\some.h" 足矣


热点排行