-2

I'm stuck at trying to pass a vector into system() function. I've read previous questions about this topic but I came out empty handed.

class aloha
{
private:
    string str;

public:
   void Caller() {
   std::string line;
   std::vector<string>vl;

   std::ifstream file("data.txt");
   while (file >> line) {
       vl.push_back(line);
       std::string c = "Taskkill /F /IM " (vl.begin(), vl.end())));
       system(c.c_str()));
       }
    }
};

I got the following errors and it has driven me nuts:

||=== Build: Debug in Instruction (compiler: GNU GCC Compiler) ===|
error: expression cannot be used as a function

error: cannot convert 'std::basic_string<_CharT, _Traits,
_Alloc>::c_str<char, std::char_traits<char>, std::allocator<char> >' from type 'const char* (std::basic_string<char>::)() const' to type
'const char*'|`

Thanks in advance, If you guys need any more info/sample code I'll put more.

Sorry I'm new to c++

kocica
  • 6,412
  • 2
  • 14
  • 35
  • maybe you didnt read carefully... anyhow there is no `string::c_st`, you probably meant `string::c_str()` – 463035818_is_not_an_ai Aug 11 '17 at 13:01
  • 7
    What is `string c = "Taskkill /F /IM " (vl.begin(), vl.end())));` supposed to do? – NathanOliver Aug 11 '17 at 13:02
  • See https://stackoverflow.com/q/1430757/10077 – Fred Larson Aug 11 '17 at 13:02
  • btw either I am missing something or your error message doesnt match the code. I suspect that in your real code you have `c.c_str` where here you have `c.c_st` – 463035818_is_not_an_ai Aug 11 '17 at 13:03
  • @NathanOliver The string contains the command "Taskkill /F /IM " and then is supposed to add the strings inside of the vector to the line. – Johannes Khazaal Aug 11 '17 at 13:07
  • *"... trying to pass a vector into system() function."* - care to explain what you *mean* by that? What is this code supposed to be *doing* ? In short, what *exactly* do you want that system call to execute with each iteration of you loop. A sample of the input file, and how it relates to what you expect to execute with `system()` is more-or-less mandatory for making your question even semi-reasonable. Edit your question with the appropriate information *please*. – WhozCraig Aug 11 '17 at 13:07
  • 1
    @JohannesKhazaal That is not how you concatinate a vector of strings. To do that see: https://stackoverflow.com/questions/1985978/combining-a-vector-of-strings – NathanOliver Aug 11 '17 at 13:11

1 Answers1

0

The line below is the line to look at.

string c = "Taskkill /F /IM " (vl.begin(), vl.end())));

You can concatenate strings with std::append http://www.cplusplus.com/reference/string/string/append/

You will need to do this in a for loop, appending each item of your vector one at a time. However, since your vector in the example only contains one element, it isn't entirely necessary for the code above. Once you have your desired string, you can pass it to system().

blake
  • 76
  • 1
  • 4