通过 Visual Studio gui 将命令行参数传递给我的 C++ 程序 [英] Passing command-line arguments to my c++ program through the visual studio gui

查看:22
本文介绍了通过 Visual Studio gui 将命令行参数传递给我的 C++ 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Visual Studio 2012,我希望用它来编写 C++ 代码.

I have just started using Visual Studio 2012, which I wish to use to write C++ code.

为了了解 IDE 的工作原理,我创建了一个非常简单的添加两个数字"程序.

To get to know how the IDE works I have created a very simple 'add two numbers' program.

#include<iostream>
#include<cstdlib>

int add(int a , int b)
{
  return a+b;
}

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

  int a = atoi(argv[1]);
  int b = atoi(argv[2]);
  std::cout << "The sum of the arguments is  " << add(a,b)      << std::endl;  
  return 0;
}

如果我对 'a' 和 'b' 的值进行硬编码(即不需要传递 argv),则使用 F7 进行编译和 Ctrl F5 运行可执行文件使程序完美运行.

If I hard-code the values of 'a' and 'b' (ie no argv's require to be passed), then using F7 to compile and Ctrl F5 to run the executable makes the program run perfectly.

但是如果我想在运行时指定它们,我如何将 argv 传递给程序?

But how do I pass the argv's to the program if I want to specify them at run time?

根据 this SO 线程上的第二个答案我必须使用项目选项卡-> 属性-> 配置属性-> 调试,然后在(命令参数)下的右侧输入,

According to the 2nd answer on this SO thread I have to use Project Tab-> Properties-> Configuration Properties-> Debugging and then enter in the Right Hand Side under (Command Arguments),

但是如果我想快速运行程序并测试不同'argv'的可执行文件,这似乎非常不方便.

But this seems very inconvenient if I want to make several quick runs of the program and test out the executable for different 'argv's.

推荐答案

您可以从 http://n0n4m3.codingcorner.net/?p=214.有 VS2010 和 VS2012 的版本.

You can try CLI Args Made Easy from http://n0n4m3.codingcorner.net/?p=214. There are versions for VS2010 and VS2012.

  1. 您可能需要将下载的文件从 .zip 重命名为 .vsix,例如:CLIArgsMadeEasy2012.vsix
  2. 双击安装.
  3. 运行 Visual Studio.
  4. 右键单击工具栏区域显示工具栏,然后单击CLIArgsMadeEasy
  5. 工具栏显示一个用于命令行参数的文本框 (CLIArgs) 和一个组合框(启动项目)
  6. 在 CLIArgs 中输入参数,记得按 Enter 键,否则参数不会被保存.
  7. 运行您的程序.
  1. You may need to rename the downloaded file from .zip to .vsix, eg: CLIArgsMadeEasy2012.vsix
  2. Double-click to install it.
  3. Run Visual Studio.
  4. Show the toolbar by right-clicking the toolbar area, and click CLIArgsMadeEasy
  5. The toolbar appears with one textbox for command line arguments (CLIArgs) and a combobox (Startup Project)
  6. Enter the argument in CLIArgs and remember to press the Enter key, otherwise the arguments will not be saved.
  7. Run your program.

这篇关于通过 Visual Studio gui 将命令行参数传递给我的 C++ 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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