By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

How to call functions from the command line

P: 18
Hi all,

I have a c++ programme that intakes a binary file and performs various operations on it. I am not gonna get in to the details of the operations, currently i specify the order in which the operations take place by calling different functions the way i write the main function. My question is -- Is there a way that a user can call different functions from this application from the command line.
For example.

say i have 3 functions e.g : Open,F1 , F2, F3.

Now i would like to call the functions while the programme is running and i understand that i wont be able to do anything else at the command line while the prograame is running. Its an application so i wanna run the programme first and then call different functions while the programme is running such as:

a.out Open (filename)
a.out CallF1 (x,y,z)
(after F1 produces its result e.g "Woohoohoo" then
a.out CallF2 (a,b,c)
.
.
.
and Finally

a.out Quit.

meaning call different functions from the command line with the parameters.
Any help with the structure of the main function to acheive the above will be highly appreciated.
Mar 27 '07 #1
Share this Question
Share on Google+
15 Replies


Expert 100+
P: 1,510
I assume when you mention command line you are talking about entering data at the keyboard while the program is executing (rather than the comand line when you start the program)??
at the simplest level you could read a number or character from the keyboard and depending upon its value select the operation you wish to do
Expand|Select|Wrap|Line Numbers
  1. a: Open (filename)
  2. b: CallF1 (x,y,z)
  3. c: CallF2 (a,b,c)
  4. .
  5.  
see
http://www.codeguru.com/cpp/tic/tic0038.shtml
for examples using for, switch, etc
Mar 27 '07 #2

P: 3
I would suggest using command-line arguments. Look up argc and argv[]. Once you've got the arguments from command-line, it should be trivial to implement a switch statement with each function in the case of its name.
Mar 27 '07 #3

P: 18
at the simplest level you could read a number or character from the keyboard and depending upon its value select the operation you wish to do
Expand|Select|Wrap|Line Numbers
  1. a: Open (filename)
  2. b: CallF1 (x,y,z)
  3. c: CallF2 (a,b,c)
  4. .
  5.  
see
http://www.codeguru.com/cpp/tic/tic0038.shtml
for examples using for, switch, etc
Thanks for your response. Actually its a bit more complicated than that. This is a utility that intakes a binary file and then performs different tasks on it based on what the user decides to do with it, moreover when the user would call a certain action (function) he would also specify many parameters for that function e.g he may wanna specify the range of words he would like to see . I already have all the functions written and the programme is complete. Before I would call the function in the main programme in the order i wanted but now i wanna make completely flexible so the user can call any action from this utlity from the commendline while the programme is running . So what I am looking for is command line statements that would allow me to do this. Therefore i need some help to set up the main function that would accept statements from the command line calling functions with the user provided parameters.

Thanks for yourtime again.
Mar 27 '07 #4

Expert 100+
P: 1,510
a few questions
(1) what operating system are you using
(2) how will you start the program, e.g. from the command line, click an icon, on system start up, on user login, etc
(3) will the program run in the background or have a window where it is displaying information
Mar 27 '07 #5

P: 18
a few questions
(1) what operating system are you using
(2) how will you start the program, e.g. from the command line, click an icon, on system start up, on user login, etc
(3) will the program run in the background or have a window where it is displaying information

I am using linux suse.
I will start the programme from the command line .
The programme will display information in the command line window.

Once again thank you very mcuh for your help and time. I appreciate it alot.
Mar 27 '07 #6

Expert 100+
P: 1,510
I am using linux suse.
I will start the programme from the command line .
The programme will display information in the command line window.

Once again thank you very mcuh for your help and time. I appreciate it alot.
will you run if from a terminal window so you can run other programs, e.g. if the program is called prog
./prog&

or so it can use the keyboard, e.g.
./prog
Mar 27 '07 #7

P: 18
will you run if from a terminal window so you can run other programs, e.g. if the program is called prog
./prog&

or so it can use the keyboard, e.g.
./prog


It will be run from the terminal window and i wont run any other prgrams from that window until i quit this program. I hope this makes it clear.Thanks
Mar 27 '07 #8

Expert 100+
P: 1,510
It will be run from the terminal window and i wont run any other prgrams from that window until i quit this program. I hope this makes it clear.Thanks
The next question is will the program execute a task and then wait for another command - if so that is simple when the currect taks is done you just disply a menu and ask for input.
If the program needs to be waiting for input at the same time as processing there are various ways of deal with this.
(1) multuthreading - the main thread handles user interaction creating theads to do the tasks as requested - threads can communicate using IPC, e.g. shared memory
(2) multiple processes; the parent forks child processes to carry out tasks. The processes can communicate using IPC, e.g. messages, pipes, etc.
(3) tatally independent processes communicating via named pipes, TCP, UDP????

it all depends what you are trying to do and how interrelated the taks are - multithreading is the simplest
Mar 27 '07 #9

P: 18
The next question is will the program execute a task and then wait for another command - if so that is simple when the currect taks is done you just disply a menu and ask for input.
If the program needs to be waiting for input at the same time as processing there are various ways of deal with this.
(1) multuthreading - the main thread handles user interaction creating theads to do the tasks as requested - threads can communicate using IPC, e.g. shared memory
(2) multiple processes; the parent forks child processes to carry out tasks. The processes can communicate using IPC, e.g. messages, pipes, etc.
(3) tatally independent processes communicating via named pipes, TCP, UDP????

it all depends what you are trying to do and how interrelated the taks are - multithreading is the simplest


Thanks for your reply. Actually the programme will execute the task and then wait for another command. I will try to explain it alittle better this time.Its a Store point binary file that I am trying to show the contents of. My programme is like a editor or a viewer that explains what every byte in that file means. I already know what is the format of the file and how it is created. based on that knowledge i have created my prgramme to show the contents of the file which are more umderstandble than 1s and 0s.

So the user will open up the file using my programme and then he may wanna see different parts of the file one by one with different parameters.Forexample he may start off with the function to display the META DATA of teh file, once that task is done i want my programme to prompt the user to enter the next command or "q" to quit. Now if he wishes to see the Core memory content of the file ,he will call that function and provide parameters (e,g what range of core memory he would like to see) once Core Memory information has been displayed again i want my programme to prompt the user to enter next command or q to quit, so on and so forth.

I hope you see what i am trying to do here. one command at a time.I just dont know what is the best way to acheieve this and how to setup the main function.I would have posted the code but its over 1600 lines.
Awaiting your reponse.Thanks for your help and time.
Mar 28 '07 #10

Expert 100+
P: 1,510
here is an example of a very simple menu system which would probably do what you require
Expand|Select|Wrap|Line Numbers
  1. // very simple command line menu
  2.  
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7.     while(1)   // execute 'forever'
  8.       {
  9.        int choice=-1;
  10.        while(choice <=0 || choice > 5)    // ignore any invalide choice
  11.           {
  12.            printf("Select from command\n"
  13.                    "  1: Open file\n  2: Process data\n  3: display results\n"
  14.                    "  4: Save file\n  5:  Exit\n   enter command (1 to 5) ?");
  15.            scanf("%d", &choice);
  16.            }
  17.        // valid choice enter, execute command
  18.        switch (choice)
  19.            {
  20.            case 1: // statements to open file
  21.                    printf("Open file\n");
  22.                    break;
  23.            case 2: // statements to process data
  24.                    printf("Process data\n");
  25.                    break;
  26.            case 3: // statements to display results
  27.                    printf("display results\n");
  28.                    break;
  29.            case 4: // statements to Save file
  30.                    printf("Save file\n");
  31.                    break;
  32.            case 5: exit(0);
  33.            default:  // should not happen!
  34.                    printf("Error!!!! %d\n", choice);
  35.             }
  36.        }
  37. }
  38.  
the user enter the command between 1 and 5 (you could use characters O, P, D, S, etc) - anything else is ignored. The command selected is executed via a switch() selection (you could have a function for each command here!) . When complete the menu is displayed again, etc, etc.
Mar 28 '07 #11

P: 18
Thanks for your code it was very helpfull. Since i have too many commands that the user can call therefore i am not in a position to show the menu everytime or assign a number to every command. What i was thinking , if i could write something that would allow the user to enter the name of the command every time the previous process is done. I have tried doing this using the following code. But its crashing randomnly, it works sometimes but other times it calls different commands than what enetered. I would really appreciate if you could take a look at it and advise.here is the chunk of the code.

Expand|Select|Wrap|Line Numbers
  1. int srange=0; //start range
  2. int erange=0; //end range
  3. int eerange=0; //another end range
  4.  
  5.  
  6.   cout<<"\nSTP:::"; //name of the utlity
  7.   cin>>command;  //this is where user will enter the command
  8.  
  9.    while (command!="q"||"Q") // while not equal to quit
  10.    {
  11.  
  12.      if (command =="SCM") {
  13.        cout<<"Please enter the Start range:";
  14.        cin>>oct>>srange;
  15.        cout<<"Please enter the End range:";
  16.        cin>>oct>>erange;
  17.        SCM(srange,erange);  //function
  18.        cout<<"\nSTP:::";
  19.        srange=0;
  20.        erange=0;
  21.        command="";
  22.        cin>>command;
  23.      }
  24.      else if (command =="SFD") {
  25.        cout<<"Please enter the Start Sector:";
  26.        cin>>oct>>srange;
  27.        cout<<"Please enter the End Sector:";
  28.        cin>>oct>>erange;
  29.        SFD(srange,erange);
  30.        cout<<"\nSTP:::";
  31.        srange=0;
  32.        erange=0;
  33.        command="";
  34.        cin>>command;
  35.      }
  36.  
  37.      else if (command =="SCDR") {
  38.        SCDR();
  39.        cout<<"\nSTP:::";
  40.        command="";
  41.        cin>>command;
  42.      }
  43.  
  44.      else if (command =="SDI"){
  45.        SDI();
  46.        cout<<"\nSTP:::";
  47.        command="";
  48.        cin>>command;
  49.      }
  50.      else if (command =="SFP"){
  51.        SFP();
  52.        cout<<"\nSTP:::";
  53.        command="";
  54.        cin>>command;
  55.      }
  56.      else if (command =="SAO"){
  57.        SAO();
  58.        cout<<"\nSTP:::";
  59.        command="";
  60.        cin>>command;
  61.      }
  62.      else if (command =="SDO"){
  63.        SDO();
  64.        cout<<"\nSTP:::";
  65.        command="";
  66.        cin>>command;
  67.        }
  68.        .
  69.        .
  70.        .
  71.       .
  72. }//endwhile
Mar 28 '07 #12

Expert 100+
P: 1,510
it looks basically OK except I don't think you can do
Expand|Select|Wrap|Line Numbers
  1.   while (command!="q"||"Q") // while not equal to quit
  2.  
try?
Expand|Select|Wrap|Line Numbers
  1.   while (command!="q"||command!="Q") // while not equal to quit
  2.  
Mar 28 '07 #13

P: 18
it looks basically OK except I don't think you can do
Expand|Select|Wrap|Line Numbers
  1.   while (command!="q"||"Q") // while not equal to quit
  2.  
try?
Expand|Select|Wrap|Line Numbers
  1.   while (command!="q"||command!="Q") // while not equal to quit
  2.  
Thanks for your reply. I have made the correction that you mentioned but it works either way. Like I mentioned before, its acting very weird, sometimes say if Function A is called it would execute say Function X. Totally random. I cant see a problem with the code or maybe something is wrong with the string comparison. You help is very appreciated. Thank you.
Mar 28 '07 #14

Expert 100+
P: 1,510
I assume your variable command is a C++ string - also check that all your comparisons are == and not =.
This little program which look similar to your idea works OK
Expand|Select|Wrap|Line Numbers
  1. // very simple command line menu
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.        string command("");;
  10.        while(command != "Q" && command !="q")    // exit on Q or q
  11.           {
  12.            printf("Select from command\n"
  13.                    "  Open, Process, Save, Q\n   enter command ?");
  14.            cin >> command;
  15.            if(command=="Open")    cout << "Open" << endl;  else
  16.            if(command=="Process") cout << "Process" << endl;  else
  17.            if(command=="Save")    cout << "Save" << endl;  else
  18.                                   cout << "Illegal command try again" << endl;
  19.            }
  20. }
  21.  
Mar 28 '07 #15

P: 18
I assume your variable command is a C++ string - also check that all your comparisons are == and not =.
This little program which look similar to your idea works OK
Expand|Select|Wrap|Line Numbers
  1. // very simple command line menu
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.        string command("");;
  10.        while(command != "Q" && command !="q")    // exit on Q or q
  11.           {
  12.            printf("Select from command\n"
  13.                    "  Open, Process, Save, Q\n   enter command ?");
  14.            cin >> command;
  15.            if(command=="Open")    cout << "Open" << endl;  else
  16.            if(command=="Process") cout << "Process" << endl;  else
  17.            if(command=="Save")    cout << "Save" << endl;  else
  18.                                   cout << "Illegal command try again" << endl;
  19.            }
  20. }
  21.  
THANK YOU VERY MUCH FOR YOUR TIME AND VALUABLE HELP.I got this working. I do have another little question though.

I would like to convert a short in to 2 chars. Since short is 2 bytes i would like to isolate them and convert them into 2 chars.

Any suggestions would be appreciated.
Thanks in advance.
Mar 30 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.