c++: argv包含一些空格
c++: argv contains some spaces
我只想传递一个包含一些空格的参数到我的函数main。下面是一个例子:
string param = "{"abc" "de"}"; // the string is {"abc" "de"}
boost::replace_all(param, """, "\""); // now it becomes: {"abc" "de"}
boost::replace_all(param, " ", "\40"); // now it becomes: {"abc"40"de"}
ShellExecute(GetDesktopWindow(), "open", "myMainTest.exe", param.c_str(), "", SW_SHOWNORMAL); // execute my function main in another project
//myMainTest.exe的main函数
cout<<argv[1];
我得到了这个结果:
{"abc"40"de"}
恕我直言,这与windows处理命令行的方式直接相关。参数通常用空格分隔,但用双引号括起来的字符串("
)在去掉引号后会作为单个参数处理。
但它与类unix shell处理输入的方式相去甚远!没有简单直接的方法来转义引用本身。但如果你的报价是平衡的,它就会起作用。这是必须传递给ShellExecute
的实际字符串:"{"abc" "def"}"
。现在只剩下如何写那是c++源码:
string param = ""{\"abc\" \"def\"}"";
ShellExecute(GetDesktopWindow(), "open", "myMainTest.exe", param.c_str(), "", SW_SHOWNORMAL);
myMainTest.exe
只能看到单个参数:{"abc" "def"}
相关文章:
- 如何在c++中使用system()来运行包含空格的python脚本
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- C++为一串单词添加空格
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- CMake:我们可以为一组不形成可执行文件或库的特定文件指定包含目录吗?
- 如何存储包含多个空格的字符串 c++
- 如何处理包含空格的 cin 的用户输入?
- 如何在C++的凯撒密码程序中包含空格?
- 如何包含整数之间的空格作为输出
- 访问包含P的有效索引时返回空格的C++字符串
- 当我在结构中包含多个数组时,我的程序会跳过一堆代码
- 搜索字符串是否至少包含一次从 0 到 9 的所有数字的最有效方法
- 至少包含一次字符 X 的 S 的不同子字符串的数量
- C++包含一组对象的类
- 我想在C++项目中包含一种脚本语言.Lua vs Bison/Yacc
- 为什么我可以在多个包含const int的cpp文件中包含一个头文件,而不会出现编译器错误
- 有没有办法将源代码格式化为每行只包含一条语句
- 双链接列表总是只包含一条记录
- 页眉应包含一次
- 头文件在整个程序中仅包含一次