473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to call functions from the command line

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
15 11279
horace1
1,510 Expert 1GB
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
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
AmmarN
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
horace1
1,510 Expert 1GB
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
AmmarN
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
horace1
1,510 Expert 1GB
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
AmmarN
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
horace1
1,510 Expert 1GB
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
AmmarN
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
horace1
1,510 Expert 1GB
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
AmmarN
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
horace1
1,510 Expert 1GB
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
AmmarN
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
horace1
1,510 Expert 1GB
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
AmmarN
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

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

Similar topics

1
by: Andrew Pasetti | last post by:
Using PHP Version 4.3.7/Apache/2.0.46 (Red Hat), I am not given access to php's socket functions. However, when I run the socket program by serving it as a web page, everything works as expected. ...
1
by: Bill Robinson | last post by:
I am trying to figure out how to run another program from within VB 6 using command line parameters. I want the VB program to be running continuously, and call the command line program at regular...
4
by: Boogie El Aceitoso | last post by:
Hi, I need a command line parser that understands filename swith spaces. Since I'm absolutelly sure I'm not the first developer to need a command line parser, I was wondering is there's a...
1
by: Nicholas Then | last post by:
I am writing an application that will call a command line application. Basically I have set up the windows 2003 POP3/SMTP service and to change a password of a mailbox, I need to execute the...
7
by: Ryan | last post by:
I was wondering if anyone could lend a hand with the following issue... I am testing application code (C++) that is currently built for the VxWorks platform. When loading in the object code (ld...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
34
by: Roman Mashak | last post by:
Hello, All! I'm implementing simple CLI (flat model, no tree-style menu etc.). Command line looks like this: <command> <param1> <param2> ... <paramN> (where N=1..4) And idea is pretty simple: ...
5
by: Selva Chinnasamy | last post by:
Hi I have a Windows application written in VB which has many functions. In addition it also reads a database and generates a output file. Currently this function is called when ever user clicks...
2
by: AmmarN | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.