LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2005, 02:12 PM   #1
twirl
Member
 
Registered: Aug 2005
Posts: 168

Rep: Reputation: 15
Help needed with system() and popen!


Hi,

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 :-

path;./startfile:Gamename aka /home/twirl/bf1942;./start.sh:Battlefield 1942

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;
}
 
Old 09-13-2005, 02:54 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
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 :-

path;./startfile:Gamename aka /home/twirl/bf1942;./start.sh:Battlefield 1942

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:
Code:
   sprintf(buff, "bunzip2 %s", argv[1]);
   system(buff);

Last edited by schneidz; 09-13-2005 at 02:58 PM.
 
Old 09-13-2005, 03:15 PM   #3
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
hi,

hmm i think i can cd into the dir by doing system(s1); and then do exec(s2); and if i put them into a loop would that work?

thankyou
 
Old 09-13-2005, 03:17 PM   #4
twirl
Member
 
Registered: Aug 2005
Posts: 168

Original Poster
Rep: Reputation: 15
OOps

I meant system(cd s1); exec(s2);

can i do something like that?
 
Old 09-14-2005, 09:22 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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

Last edited by schneidz; 09-14-2005 at 09:24 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
popen for windows csfalcon Programming 6 11-30-2005 08:21 AM
popen output not appear dimsh Programming 4 10-22-2005 02:56 AM
Multithreading and os.popen in wxPython wapcaplet Programming 0 12-29-2004 07:58 PM
popen read and write (both) how C++Boar Programming 2 07-05-2004 01:19 PM
C Popen gold5angel Programming 2 04-17-2004 07:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration