What is the Command-Line Argument for???

Reply

Join Date: Oct 2006
Posts: 12
Reputation: doraemon is an unknown quantity at this point 
Solved Threads: 0
doraemon doraemon is offline Offline
Newbie Poster

What is the Command-Line Argument for???

 
0
  #1
Oct 21st, 2006
I recently learned the command-line argument from my book, and I understand that both of argc and argv. HOWEVER, I don't know when I will use them and for what purpose??? I appreciate if anybody can help me!!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: What is the Command-Line Argument for???

 
0
  #2
Oct 21st, 2006
It is used when you want to control your program from the command line. Rather than promting the user for input, say for example entering a filename, you can use a switch like -f filename.txt and that will be all the program needs for execution. The command line arguments are particularly useful for automated testing. If you have a lot of inputs to test, you can't always enter them by hand. So you can create a bash or dos script that creates the appropriate commandline for each input and run the program by passing those arguments.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 12
Reputation: doraemon is an unknown quantity at this point 
Solved Threads: 0
doraemon doraemon is offline Offline
Newbie Poster

Re: What is the Command-Line Argument for???

 
0
  #3
Oct 21st, 2006
Thank you for your help. It took me a long time trying to understand what you said. Finally, I understand the meaning of the program its in my book. The program named add (which add two numbers and output the result) , and I wrote that program and use command-line to test. It works!! The sample program named add is
  1.  
  2. #include<iostream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main( int argc, char *argv[])
  6. {
  7. double a, b ;
  8. if (argc != 3)
  9. {
  10. cout << "Usage : add num num \n";
  11. return 1;
  12. }
  13. a = atof ( argv [1] );
  14. b = atof ( argv [2] );
  15. cout << a + b ;
  16. return 0 ;
  17. }

But, I got some other problems...
The sample program add in my book is designed for use in command-line (at least I think that way). So, if I want to test some program from the command-line, should I always include for example like this part in my program?
  1. int main( int argc, char * argv[])
  2. a = atof ( argv [1] ); b = atof ( argv [2] );

And also in your explanation, you put -f filename.txt . This is text filename. What I did is just put filename and 2 numbers (add 100 200), and that files extension is .exe. I tried to use text file which I simply typed add 100 200 in the note pad and saved it with the extension .txt and put that file name in the command-line. Then what is does is just to open that file...... ( I know its stupid...)

I think I still can't fully understand what you said....(I got some)
If you have time, please help me.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,582
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: What is the Command-Line Argument for???

 
0
  #4
Oct 21st, 2006
Originally Posted by doraemon View Post
So, if I want to test some program from the command-line, should I always include for example like this part in my program?
  1. int main( int argc, char * argv[])
  2. a = atof ( argv [1] ); b = atof ( argv [2] );
Those things are included there becoz whatever we accept from the command is passed to the program in the form of char* (old C style strings) -- thats why the main() prototype contains char* argv[] which means argv is an array of char* or C style strings.

atof is used since what we want to do is add two floating point values but they are supplied by the user in String format -- so we need to convert from string to float so that they can be added in a natural way.

And also in your explanation, you put -f filename.txt . This is text filename. What I did is just put filename and 2 numbers (add 100 200), and that files extension is .exe. I tried to use text file which I simply typed add 100 200 in the note pad and saved it with the extension .txt and put that file name in the command-line. Then what is does is just to open that file...... ( I know its stupid...)
The filename specified at the command line is also accepted by the prog as C style string. So inorder to make use of the data present in the file, you need to open the file with the file name specified on the command, pull the values present in the file and add them together. Just specifying the file name wont do the job. Think of command line arguments as another way of passing arguments to the program just like "Wolfpack" said.

Related to this explanation see
http://www.d.umn.edu/~gshute/C/examples/myecho.C.html
http://www.cprogramming.com/tutorial/lesson14.html
http://goforit.unk.edu/cprogram/c_105a.htm (excellent sample)

Hope it helped, bye.
Last edited by ~s.o.s~; Oct 21st, 2006 at 1:17 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: What is the Command-Line Argument for???

 
0
  #5
Oct 21st, 2006
The best thing for you to do is to design a command line argument handling program by yourself. Maybe you will find one in the exercises of your book. If you find one, do it and post it here with the question for us to check. If you can't find one tell me and I will give you an exercise
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 12
Reputation: doraemon is an unknown quantity at this point 
Solved Threads: 0
doraemon doraemon is offline Offline
Newbie Poster

Re: What is the Command-Line Argument for???

 
0
  #6
Oct 21st, 2006
Thank you for all your help. I will keep studying my book and if I can find exercise program, I will do it or try writing one myself. The way to become programmer is far away for me......but I will not give up!!! Thank you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC