问题
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