how to put the whole program in a infinite loop controlled by command line arguments [closed]

廉价感情. 提交于 2020-01-17 17:44:53

问题


Programming Language : C

I'd like to put my program controlled by command line arguments..

I mean, unless I enter "quit" it should keep on executing based upon the arguments I enter to do..


回答1:


If I understand your question and comment right, you want to check for input inside a loop, and if it's "quit" you should exit the program (not the loop)?

The functions you need to look up are scanf and strcmp:

while (1)
{
    char input[256];

    /* Do some things here... */

    scanf("%s", input);
    if (strcmp(input, "quit") == 0)
        break;  /* exit loop */
}



回答2:


Reading your question literally gives me this:

int main(int argc, char **argv)
{
    for(int i = 0; i < argc; ++i)
    {
        if(strcmp(argv[i], "quit") == 0)
            break;
        doSomething(argv[i]);
    }
}

Not really an infinite loop, but if you're really going after command line arguments, an infinite loop is probably not reasonable. But I'm 99% certain you're really looking for Joachim Pileberg's answer (i.e. not using command line arguments, but getting input from the standard input stream).



来源:https://stackoverflow.com/questions/8010514/how-to-put-the-whole-program-in-a-infinite-loop-controlled-by-command-line-argum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!