C ++:如何使用命令行参数 [英] C++: How to use command line arguments

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

问题描述

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

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

int main(int argc,char * argv [ )

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.

所以我知道打开我的程序,唯一的方法是打开它通常像我

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

现在这里似乎是为了使用命令行参数,程序必须以不同的方式打开,使用命令行(例如DOS /命令提示符),然后写入参数。

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

所以我的问题是

如何使用命令行打开我的程序,如何在程序名称后输入参数?

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天全站免登陆