| | |
What is the Command-Line Argument for???
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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. バルサミコ酢やっぱいらへんで
•
•
Join Date: Oct 2006
Posts: 12
Reputation:
Solved Threads: 0
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
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?
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.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstdlib> using namespace std; int main( int argc, char *argv[]) { double a, b ; if (argc != 3) { cout << "Usage : add num num \n"; return 1; } a = atof ( argv [1] ); b = atof ( argv [2] ); cout << a + b ; return 0 ; }
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?
C++ Syntax (Toggle Plain Text)
int main( int argc, char * argv[]) 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.
•
•
•
•
So, if I want to test some program from the command-line, should I always include for example like this part in my program?
C++ Syntax (Toggle Plain Text)
int main( int argc, char * argv[]) a = atof ( argv [1] ); b = atof ( argv [2] );
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...)
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.
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
バルサミコ酢やっぱいらへんで
![]() |
Similar Threads
- Command-line argument syntax (C++)
- command line arguments help (C)
- To fetch data through command line arguments (C++)
- command line arguments problem. (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: program dying?? any ideas why?
- Next Thread: C++ Help....plez.
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data database delete desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






