954,202 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with system() function...

hello there, im having trouble with the system() function...as much as possible, i would like my program to be dynamic...i created a program that will make an iso file...it will ask the user to input the directory that he would like to compress...the problem is, how do i append this to the system() function...here is my code...

_________________________________________

#include <stdlib.h>
#include <iostream>

using namespace std;


const char *directory;

int main(){

         cout << "enter directory name: ";
         cin >> directory;
         system("ls -l");
         system("mkdir"+directory);
         system("mkisofs -J -o filename.iso "+directory);

return 0;

}
______________________________________________

notice the + directory?? lol...it's obviously wrong...help pls...

jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
 

The easiest way is to make a C++ string and use its concatenation operator (which happens to be the plus operator). Use the c_str method to get the C-style string that's similar to the C++ string you've concatenated that the system function needs.

#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;


int main(){

  string directory;

  cout << "enter directory name: ";
  cin >> directory;
  system("ls -l");
  system(("mkdir " + directory).c_str());
  system(("mkisofs -J -o filename.iso " + directory).c_str());

  return 0;

}
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

If all that you are going to do is to use C++ to
make calls to commands to the CLI , why don't
you just make a script in bash with those commands?.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
If all that you are going to do is to use C++ to make calls to commands to the CLI , why don't you just make a script in bash with those commands?.

im going to make this as a function to my other c++ program...i was just testing it... :D

jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You