简体   繁体   English

输入C ++捕获命令行并逐行输出

[英]C++ capture command line entered and output line by line

I'm writting a c++ program and I am looking to capture the inputs from command line. 我正在编写一个c ++程序,并且希望从命令行捕获输入。 For each argument that is entered in the command line it is to be captured. 对于在命令行中输入的每个参数,将对其进行捕获。 The program will capture every command line until the user enters END. 该程序将捕获每个命令行,直到用户输入END。 The program will then provide an output of each commandline argument that was entered. 然后,程序将提供所输入的每个命令行参数的输出。 Example: User enters ./program to run this program. 示例:用户输入./program来运行该程序。 ./program is captured. ./程序被捕获。 User then enters test this is then captured user then enters arg then END . 用户然后输入test ,然后被捕获的用户然后输入arg,然后输入END The program then outputs 然后程序输出

  • ./program 。/程序
  • test 测试
  • arg 精氨酸

This is what I found for a resource I just don't know how to fully implement and loop http://www.cplusplus.com/articles/DEN36Up4/ 这是我发现的资源,我不知道该如何完全实现和循环http://www.cplusplus.com/articles/DEN36Up4/
Below is what I have so far, just not sure of the for loop waiting for the END thanks! 以下是我到目前为止的内容,只是不确定for循环是否在等待END谢谢!

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    std::cout << "Enter your command line \n and to stop enter END" << std::endl;
    for (int i = 0; i < count; ++i)
    {
        /* code */
    }
    // Once user types END then arguments entered in will display
    std::cout << << std::endl;
    return 0;
}

Take a look at cin and getline 看看cingetline

Sample code (not tested): 示例代码(未经测试):

string command_list;

while(true) {
    string input;
    getline (cin, input);

    if(input.compare("END") == 0)
        break;
    else
        command_list.append(input);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM