C++系统功能"not reconized as internal or external command"
C++ System function "not reconized as internal or external command"
代码如下:
#include <iostream>
#include <string>
using namespace std;
int main(){
cout << "type dir" << endl;
string command;
cin >> command; //typed C:Java
const char* cml = ("cd C:" + command).c_str();
system(cml);
cout << "[System]: Set!";
}
,结果如下:
'exe' is not recognized as an internal or external command,
operable program or batch file.
如果我只是键入system("cd C:Java");,那么它就工作了。但是如果我像上面那样将const char传递到系统函数中,我会得到一个错误,即无法识别exe。
这是未定义的行为:
const char* cml = ("cd C:" + command).c_str();
变量cml
正在被初始化,其C-string指针来自std::string
,作为连接的结果返回。然而,这个结果是一个临时变量,在语句结束后被丢弃。因此,cml
指向的字符数组指针在这行之后已经被释放了。
下面的语句将保留连接结果,以便在接下来的语句中使用:
string cml = "cd C:" + command
system(cml.c_str())
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- CppSQLite 抛出"LNK2019: unresolved external symbol"错误
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- 模式"allocate memory or use existing data"
- N-API include an .so or dll
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 将 OR 逻辑运算符从 C++ 转换为 Fortran
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- 我如何将 OR 与 if 语句一起使用字符串
- Visual Studio中的wxwidgets应用程序给出错误"LNK2019 unresolved external symbol"
- 将 if 语句中的字符与 or 进行比较
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- "No such file or directory" C++标头问题
- 处理 std::enable_if<...中谓词的逻辑"OR">
- C++ 虽然语句不适用于字符串和"or"
- 错误"<url> is not recognized as an internal or external command, operable program or batch file.
- C++系统功能"not reconized as internal or external command"