如何在C ++程序中使用命令行参数? [英] How to use command line arguments in C++ program?

查看:83
本文介绍了如何在C ++程序中使用命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道要使用命令行参数,我必须这样做。

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:


命令行参数在
命令行操作系统(如DOS或Linux)中的程序名称之后给出,并传递给
是从操作系统中获取的程序。

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.

所以我知道打开程序的唯一方法就是像我一样打开程序会开始调试或打开exe文件。

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

现在看来,要使用命令行参数,必须以不同的方式打开程序,使用命令行(例如Windows命令提示符),然后在其后写参数。

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.

所以我的问题是

如何使用命令行打开程序,以及如何输入arg

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

推荐答案

为简单起见,我假设您使用的是Windows 7。

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

最简单的方法是打开DOS框,然后将应用程序拖放到该框上。这将插入可执行文件的路径。之后,您可以开始键入您希望传递的命令行参数。最终应该看起来像这样:

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

注意:如评论中所述,这在Windows Vista上不起作用,这在撰写本文时我还没有意识到。

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

这篇关于如何在C ++程序中使用命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆