【问题标题】:Imitating standard user input in c++ with command line arguments使用命令行参数模仿 C++ 中的标准用户输入
【发布时间】:2014-09-23 19:33:27
【问题描述】:

假设我有以下 C++ 代码

#include <iostream>
int main()
{
   int x,y;
   std::cout << "Please enter the first input." << std::endl;
   std::cin>>x;
   std::cout << "Please enter the second input." << std::endl;
   std::cin>>y;
   std::cout<<x/y<<std::endl;
   return 0;
}

我可以用cl/EHsc sample.cpp从命令行编译文件

我想要做的是用命令行给出的输入显示程序的输出。我该怎么做?

x 应该从第一个命令行参数中获取它的值,

y 应该从第二个命令行参数中获取它的值。

以下应该可以,但我想避免摆弄 Visual Studio 属性等。

Piping input into a c++ program to debug in Visual Studio

编辑:另外为了进一步澄清,我想将它用作一个自动化系统,它从命令行接收输入,我不想修改原始代码

【问题讨论】:

  • argv[1] 和 argv[2] 不起作用?
  • 我应该补充一点,我不想修改代码本身,我编辑了问题,这是一个有效的观点。
  • 写一个shell脚本包装器?
  • 您能否详细说明或举个例子,shell 脚本包装器如何将命令行参数传递给可执行文件(作为标准输入)?
  • echo "arg1 arg2" | ./yourprogrnamehere(等等,VS/Windows;nm)

标签: c++ visual-studio debugging


【解决方案1】:

您链接到的“管道输入”问题的公认答案也适用于您的问题。如果您使用命令行窗口来执行此操作,则不必摆弄 Visual Studio 属性。只需打开命令提示符并输入:

cd c:\path\to\project\debug
sample.exe < my_input.txt

编辑:WhozCraig 的建议也有效:

cd c:\path\to\project\debug
echo 6 2 | sample.exe

【讨论】:

    猜你喜欢
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2014-02-19
    • 2015-07-20
    相关资源
    最近更新 更多