how to execute system command from textfile

Hello any help about this code?
this code read a command from text file but when i try to execute it using "system" i get error see the code bellow.

c++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("cmd.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      system(line); //<-- im getting error here
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
  system("PAUSE");
  return 0;
}


my text file content
--> del mydoc.doc <--

filename
--> cmd.txt <--
Does the command "del mydoc.doc" work otherwise?

If it does, then do cout<<line before the call to system to check the validity of the string. If the string is not correct, then it's a file read issue.
Last edited on
getline (myfile,line);
system("del mydoc.doc") //< this code work to delete mydoc.doc
system(line); //<-- but this one is not working.
cout << line << endl; //<-- also this one working it prints "del mydoc.doc"

also

i trying this code

command = "del mydoc.doc";
system(command); //<-- not working
Last edited on
system( line.c_str() ); //a "string" is not a "char*"
it is working now thanks.. Mathhead200
Topic archived. No new replies allowed.