Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use command line arguments in C++ program?

I know that to use command line arguments, I have to do this.

int main(int argc, char* argv[])

Now most of the documentation i read about taking in command line arguments explain the situation, something like this:

Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.

So the only way i know to open my program, is to open it normally like i would do, either start debugging or open the exe file

Now here it seems that, to use command line arguments the program has to be opened differently, Using the Command-Line (Windows Command Prompt for example), and then write the arguments after it.

So my question is

How do i open my program using the Command-Line, and how do i enter the arguments after the program name?

like image 258
Mohamed Ahmed Nabil Avatar asked Dec 08 '22 21:12

Mohamed Ahmed Nabil


2 Answers

For the purpose of simplicity, I will assume you are using Windows 7.

The simplest way is to open a DOS box and then drag-n-drop you application on to it. This will insert the path to your executable. Following that, you may begin typing the command line arguments you wish to pass it. It should end up looking something like this:

C:\Users\cscott> "C:\Users\cscott\Documents\myApp.exe" argument1 argument2

Note: As mentioned in the comments, this does not work on windows vista, a fact I was unaware of at the time of writing.

like image 80
Tsubashi Avatar answered Dec 22 '22 01:12

Tsubashi


I'm going to assume you're using an IDE, and I'll take a wild guess that it's Visual Studio. If I'm right, there are two approaches - one, open up the folder containing the executable that's been built - it'll be in {Solution Directory}/{Project Directory}/bin/{Build Configuration} by default. Run the command line there. The other option is to open the project properties, and under the "Debug" tab (in VS 2010 - it varies by version) put your command line flags in the box labeled "Command line arguments".

like image 30
Harper Shelby Avatar answered Dec 22 '22 00:12

Harper Shelby