Mar 12, 2009 at 9:32am UTC
All,
I need to know how do i pass arguments on cmd to a CPP program using int
main(int argc, char * argv[])
Please help me with anything, source code or just an explanation.
Thanks in advance
Mar 12, 2009 at 12:26pm UTC
A program example from "C++ Without Fear", by Brian Overland
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int main(int argc, char *argv[])
{
int c; //input character
int i; //loop counter
char filename[81];
char input_line[81];
if (argc > 1)
strncpy(filename, argv[1], 80);
else {
cout << "Enter a file name and press ENTER\n" ;
cin.getline(filename, 80);
}
ifstream file_in(filename);
if (! file_in){
cout << "File " << filename << " could not be opened." ;
return -1;
}
while (1){
for (i = 1; i <= 24 && ! file_in.eof(); i++){ //24=number of lines displayed
file_in.getline(input_line, 80);
cout << input_line;
}
if (file_in.eof())
break ;
cout << "\n" ;
cout << "More? (Press Q and ENTER to quit.)" ;
cin.getline(input_line, 80);
c = input_line[0];
if (c == 'Q' || c == 'q' )
break ;
}
return 0;
}
This program opens a textfile.
So you could type:
c:\code\readtxt.exe c:\lovecraft\callofcthulhu.txt
...assuming those were the correct paths
Last edited on Mar 12, 2009 at 12:29pm UTC
Mar 26, 2009 at 9:32am UTC
thanks you very guys ,i was ill so that i could not repond u .please tell me how can i put the filename ,is this is a path or just i have to put the file name
Mar 26, 2009 at 9:44am UTC
once you've written your program, open up cmd or the shell you're using and go to the directory of your program. then type the name of your program and the arguments being passed to it.
does this answer your question?
Mar 26, 2009 at 10:12am UTC
as a beginner ,i was little bit confused that ,what should i put in place of filename ,is path of directory ?
Mar 26, 2009 at 10:35am UTC
i have check the above program .please any body tell how can i open the desire file only giving his name .my objective is put any cmd comand as a argument to the progrma
Mar 26, 2009 at 10:44am UTC
my objective is take cmd commands as a arguments in the main ,please any body solve my problem
Mar 26, 2009 at 3:32pm UTC
You say you want to take cmd commands as arguments but you need to be specific about what arguments they are going to be first.