Passing arguments to main using command line

Hey everyone,

I am stuck in this whole concept of using the command line to pass arguments(or parameters, dont know which is the correct name) to main().

I have input and ouptut files that need to be supplied to my program on the command line, which i will access using int argc, char *argv[].

I use Quincy as my IDE, so what do i need to tpye on the command prompt to pass my input and output files to my program.(i know the text files names go somewhere but the rest?)


C:\Users\Admin>.......
Last edited on
program.exe argument1 argument2 "argument3 with spaces" "argument4 with \"quotes\""

EDIT: I'm not 100% sure about the quotes example.
Last edited on
use a for and printf() and see what are the arguments... example:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main(int argc, char ** argv){
	int i;

	for(i=0;i<argc;i++)
		printf("Argument %d: %s\n", (i+1), argv[i]);
	
	return 0;
}
so after \Admin>program.exe argument1 argument2........?
Last edited on
Yes, after you type program.exe, you should type the command line arguments with spaces in between.

Say you want two arguments, one for the input file and one for the output file. You would type something like this at the command line:

program.exe C:\Users\Admin\Desktop\file1.txt "C:\Users\Admin\Desktop\A Folder\file2.txt"

Notice how the second argument has quotes because it has a space.

If you type the above, argv[1] would contain "C:\\Users\\Admin\\Desktop\\file1.txt" and argv[2] would contain "C:\\Users\\Admin\\Desktop\\A Folder\\file2.txt". argv[0] would contain the full path to the executable. Backslashes need to be escaped in a string so that they don't invoke some undesirable escape sequence. You only need to put single backslashes in the command line.
Last edited on
So i go to start, command prompt, there I have C:\Users\Admin> now i just add to that
program.exe C:\Users\Admin\Desktop\file1.txt "C:\Users\Admin\Desktop\A Folder\file2.txt"

i did this and i got a message saying " 'program.exe' is not recognized as an internal or external command, operable program or batch file"
Last edited on
You need to navigate to the folder containing program.exe first.

So, if your exe is in C:\Projects\Cpp\Playing With The Command Line\Debug\Output, then you should type the following before typing program.exe ...

cd "C:\Projects\Cpp\Playing With The Command Line\Debug\Output"
Last edited on
Not to mention that program.exe is just a placeholder name we made up. You should type in the name of your own program.
Good call. Didn't notice that :)
I knew it was a place holder, anyways, i think i finally got it. I did what you said and received a message saying, something like:

Argument1: (the place of my program, it ends with program.exe), Argument2: (tha place of my output file, it ends with output.txt) Argument3: (the place of my input file, it ends with input.txt)

So it basically sends 3 arguments to the program even though i only wanted to send the input and output file??
Last edited on
argv[0] is the name that was used to start the program; it's always passed. The first actual parameter, if there was one, is argv[1].
Last edited on
so every time i work on the program and test to see if it is working or not, do i have to go back to

the command line and type the same thing all over again??

When i pass the 2 arguments to my program ( 3 if you count the location of the program) through the command line, the program runs right there in the command prompt. It shows me the 3 arguments (argv[0] argv[1] argv[2])
However, when i go back to the IDE (Quincy) and try to run the program it displays only 1 argument - the location of the program

Last edited on
The command line interpreter has a history function. If you press up or down, you can navigate the history to re-enter some past command.

Most if not all IDEs have a setting that let you specify what arguments to pass to the program when you debug. It's usually with other debug options, such as the working directory and environment variables.
Topic archived. No new replies allowed.