ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
my book dont cover system or popen enough for what i need to do, my book only mentions system but dont say how to use it and dont even mention popen.
I would like to be able to use either system() or popen to enter the path of a user specifys what is stored in games.db and then execute the command next to path what is stored in games.db at the moment i have my games.db file as :-
and i would like system to enter the path what is specified in games.db and then execute ./start.sh for example and continue doing that throughout the games.db file.
My code is below :-
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int i=0;
char s1[80],s2[40],s3[90],s4[256],s5[90]; // set char size for input
ofstream outfile("games.db", ios::app);
string whole,p1,p2;
ifstream infile;
// Start Main Menu
char choice='\0';//NULL;
bool finished=false;
while (finished != true)
{
cout<<"MAIN MENU\n";
cout<<"1. AddGame\n";
cout<<"2. Options\n";
cout<<"3. RunGame\n";
cout<<"4. Exit\n";
cout << "Please enter your choice: ";
cin>>choice;
cout<<"\n\n";
// Start function for submenu
switch(choice)
{
case '1': cout << "1. Add Game To DB\n";
cout << "2. Go back\n";
cout << "Please enter your choice: ";
cin>>choice;
if(choice == '1')
{
cout << "\n\nPlease enter full-path to game: ";
cin.get();
cin.getline (s1, 80); // Input goes in
outfile << s1 << ";"; // add ; after each path
cout << "\n\nPlease enter name of startup file: e.g ./start.sh ";
cin.get();
cin.getline (s2, 40); // Input goes in$
outfile << s2 << ":"; // add : after each startup file
cout << "\n\nPlease enter game name: ";
cin.getline (s3, 90);
outfile << s3 << "\n";
if (outfile.is_open())
{
outfile.close();
}
}
/* Just break if choice != 1 */
break;
case '2': cout<<"Options\n";
cout<<"1. Edit Game Config\n";
cout<<"2. Go back\n";
cout << "Please enter your choice: ";
cin>>choice;
if(choice=='1')
{
cout << "Please enter full path to config file to edit: "; // ask user for path to config file
cin.get();
cin.getline (s4, 256); // input goes in
string cmd = string("pico ") + string(s2);
system(cmd.data()); // open up pico to edit config
}
case '3': cout<<"Run Game";
i=0;
infile.open ("games.db", ios::in);
while(getline(infile,whole)){
i++;
p2=whole.substr(whole.find(':')+1,whole.length());
cout << i << ". " << p2 << endl;
}
cout << "Please select game you wish to run: " << endl;
cin.get();
cin.getline(s5, 100);
i=1;
infile.open ("games.db", ios::in);
while(getline(infile,whole)){
i++;
if(atoi(s5)!=i)
continue;
p1=whole.substr(0,whole.find(':'));
}
system ( string(p1+" > system_errors").c_str() );
// Here we would check 'choice' and probably move to another functi$
cout<<"\n\n";
break;
// Here we would check 'choice' and probably move to another functi$
cout<<"\n\n";
break;
case '4': //Finished so set finished 'true' to break out of loop
finished=true;
cout<<"Exiting....\n";
break;
default: //If choice !=1/2/3 then do what it says here
break;
outfile.close(); // close file games.db
// end functions
}
choice='\0'; //NULL
}
return 1;
}
I would like to be able to use either system() or popen to enter the path of a user specifys what is stored in games.db and then execute the command next to path what is stored in games.db at the moment i have my games.db file as :-
and i would like system to enter the path what is specified in games.db and then execute ./start.sh for example and continue doing that throughout the games.db file.
you would need to call system in a loop for each line in games.db
Code:
man system
system("cd /home/twirl/bf1942; ./start.sh");
also, look at 'man sprintf' to cat your command line to system. e.g:
when you call 'exec' in a loop not only will it break out of the loop, it will break out of the program. exec replaces the new programs memory with the current memory address and you cant get it back.
usually you would fork and if not parent exec.
Quote:
from hko:
This "fork() a child proces, exec() another program, and wait for it to finish" is needed quite often. Therefore there is a library function that does all of this at once: system()
basically what i was alluding to in my previous post is that you need to parse the string argument passed to 'system' somehow. (system is the equivelent of manually typing in something in the shell).
p.s. - don't use 'cd', just execute the /fully/qualified/path/name
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.