Having trouble understanding Command-line arguments

Here is how my book explains it.
All the programs in Chapter 8 operate on files. The first thing each of these programs
does (after declaring variables) is prompt the user for a filename.

cout << "Enter a file name and press ENTER: ";
cin.getline(filename, 80);

This works, but it’s not always the ideal solution. People using the DOS command
line—or another command-line–based system—would usually prefer to
enter the filename directly. For example:

readtxt output.txt

You can implement this behavior by using the command-line argument feature
of C++. The first thing you do is to define main differently. If you’re using
the Dev-C++ environment, it already provides these arguments for you.

int main(int argc, char *agrv[]) {
// ...
}



That's all good, but what is a command like argument? What does it do? It never really explains it. I mean, I'm completely in the dark on this right now. Can someone explain better? I know argc is the number of arguments, and agrv is a string that stores the arguments. What ARE these command line arguments? For an exercise it asks me to make a program that prints nothing but the command line arguments, I have no clue where to start.
Last edited on
Do you know what a command prompt is? If not google it! (If your not running Linux google "shell" instead.)
Ok, that's what I thought. Then what is my book talking about?
if your program is called "program.exe" within the windows command prompt (or equivalent) change directory until you get to the location of "program.exe" then if you type in "program.exe filename.txt" it will lunch your program and "filename.txt" will be a command line argument. Then from here you can use this to do whatever you want within your code using argv[1] to refer to it. Hope that makes sense. Let me know if it doesn't

Meerkat
DOS command line—or another command-line–based system
I.e. a command prompt or shell.
if your program is called "program.exe" within the windows command prompt (or equivalent) change directory until you get to the location of "program.exe" then if you type in "program.exe filename.txt" it will lunch your program and "filename.txt" will be a command line argument. Then from here you can use this to do whatever you want within your code using argv[1] to refer to it. Hope that makes sense. Let me know if it doesn't


That's what I was looking for! Thanks for explaining that better. =]
No problem.
Topic archived. No new replies allowed.