[英]How to pass character array to a function in C++ without any temporary char array as variable?
[英]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);
arraystring
是std::string
的數組,給出錯誤。
更改為:
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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.