如何在c++中的system()函数中传递字符数组
How to pass character array in system() function in c++?
我想使用c++平台执行linux命令,我使用的是system(),但当我编写时:
cout<<"Enter the command"<<endl;
cin>>arraystring[size];
system(arraystring.c_str());
它会出错!
a4.cpp: In function ‘int main()’:
a4.cpp:75:20: error: request for member ‘c_str’ in ‘arraystring’, which is of non-class type ‘std::string [100] {aka std::basic_string<char> [100]}’
system(arraystring.c_str());
我该怎么做才能克服这个错误?
实际上,您已经将arraystring
变量定义为:
string arraystring[100];
因此,当您编写语句时
system(arraystring.c_str);
作为std::string
的阵列的arraystring
给出了误差。
更改为:
system(arraystring[size].c_str());
您可以使用:
std::string command;
std::cout << "Enter the command" << endl;
std::cin >> command;
system(command.c_str());
只要command
只是一个单词,它就会起作用。如果你想使用多字命令,你可以使用:
std::getline(std::cin, command);
system(command.c_str());
相关文章:
- 第 i 个元素返回 i 的函数数组
- 使用嵌套函数数组是个好主意吗?
- 正确实现类函数数组
- 试图引用已删除函数数组的相等运算符
- 如何在静态函数中使用成员函数数组
- 无法创建函数数组
- 构造函数数组
- 使用开关或定义函数数组 - 更快
- 编写一个函数,该函数将覆盖主函数数组中的值
- 可以像'funs[1]();'一样调用的函数数组
- std::AVX内部函数数组
- 如果数组大小未知,如何将函数数组作为参数传递
- 从函数数组中获取值
- 使用元编程在编译时初始化函数数组
- 将C函数数组转换为C++函数指针数组
- 复制构造函数 [数组] C++
- 添加函数数组并随机选择其中一个
- 指针到私有成员函数数组
- 可以单独声明函数数组的元素
- (c++)将main中的随机值保存到函数数组中