Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当系统路径作为参数传递给函数时,无法获取转义字符_C++_Visual Studio 2008 - Fatal编程技术网

C++ 当系统路径作为参数传递给函数时,无法获取转义字符

C++ 当系统路径作为参数传递给函数时,无法获取转义字符,c++,visual-studio-2008,C++,Visual Studio 2008,我必须为我的程序传递视频文件的路径,以便函数读取传递的参数并播放文件。但是现在发生的事情是,如果我在解析路径“G:\sun\play.wmv”后,将其取为“G:sunplay.wmv” 在.cpp文件中,我编写了以下代码: int main(void) { VideoPlayer h; h.Load("G:\Sunny Cpp-2\edit.wmv"); h.Start(); getch(); return 0; } 使用的头文件是用户定义的,其中的函数是: bool V

我必须为我的程序传递视频文件的路径,以便函数读取传递的参数并播放文件。但是现在发生的事情是,如果我在解析路径“G:\sun\play.wmv”后,将其取为“G:sunplay.wmv”

在.cpp文件中,我编写了以下代码:

int main(void)
{
  VideoPlayer h;

  h.Load("G:\Sunny Cpp-2\edit.wmv");
  h.Start();
  getch();
  return 0;
}
使用的头文件是用户定义的,其中的函数是:

bool VideoPlayer::Load(const char* pFilePath)
{
   //internal coding
}
现在参数pFilePath中的值是“G:Sunny Cpp-2edit.wmv”。因此,函数无法从系统中读取路径。应该进行哪些修改以使其正常工作?
任何帮助都可以接受。提前感谢。

符号“\”表示简单转义序列,例如新行字符“\n”或引号“\”。要区分转义序列和字符“\”,最后一个必须加倍为“\”

而不是

"G:\Sunny Cpp-2\edit.wmv"
或者使用原始字符串文字

R"(G:\Sunny Cpp-2\edit.wmv)"
这里有一个例子

#include <iostream>

void f( const char *s ) { std::cout << s << std::endl; }

int main() 
{
    f( "G:\\Sunny Cpp-2\\edit.wmv" );
    f( R"(G:\Sunny Cpp-2\edit.wmv)" );

    return 0;
}

\
是一个特殊字符。它用于赋予其他字符(如
\n
)特殊含义(它赋予“n”特殊含义,组合被视为换行符)。因此,如果您想使用“\”而不具有特殊含义,则必须通过使用另一个“\”来专门化if。因此,请使用
G:\\Sunny Cpp-2\\edit.wmv“

您根本没有使用任何转义字符。事实上,这就是问题所在!我认为原始字符串文本只存在于C++11或更高版本中。如果您不使用该解决方案,请使用其他解决方案。我使用的是Visual Studio 2008,您能建议我使用该解决方案吗@GeorgeT@user3615925请看我的帖子。这就是你所需要的。
#include <iostream>

void f( const char *s ) { std::cout << s << std::endl; }

int main() 
{
    f( "G:\\Sunny Cpp-2\\edit.wmv" );
    f( R"(G:\Sunny Cpp-2\edit.wmv)" );

    return 0;
}
G:\Sunny Cpp-2\edit.wmv
G:\Sunny Cpp-2\edit.wmv