1

Programming Language : C

I'd like to put my program in a infinite loop controlled by command line arguments..

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

user1027046
  • 293
  • 1
  • 6
  • 11
  • Basically you read from comand line, parse the commands so you can filter command and arguments, invoke the command, read from the line again, put that in a infinite loop. Command design pattern would be helpful. http://www.dofactory.com/Patterns/PatternCommand.aspx – Birey Nov 04 '11 at 14:15
  • Exact duplicate of the (closed!) http://stackoverflow.com/questions/8010514/how-to-put-the-whole-program-in-a-infinite-loop-controlled-by-command-line-argum – Sjoerd Nov 05 '11 at 13:21

3 Answers3

1

Without knowing anything about your target platform, it is hard to make specific recommendations. But one way you can do it is with a "state machine." Here is a rather nice stackoverflow question that can give you some ideas. In particular look at this answer.

Community
  • 1
  • 1
Angelo
  • 2,936
  • 5
  • 29
  • 44
  • Actually, its a kind of game. where the program open a square grid and I have to give commands to the grid so as to put a cross mark in a specific square. after every command I give to it, it should execute it and ask me for another.. unless I give "quit" command to it.. the program shouldn't terminate. actually, I couldn't able to understand what's there is the link. – user1027046 Nov 04 '11 at 14:47
  • The words "here" and "this" in my answer are hyperlinks to another stackoverflow question. – Angelo Nov 04 '11 at 14:57
  • yeah! I was referring them,.. I couldn't understand. – user1027046 Nov 04 '11 at 15:10
  • FWIW, *every useful program* is a state machine. ("Hello world" might or might not qualify, but i'm not getting into that argument.) If there's looping or recursion, or if you otherwise ever act based on the values of variables, you have a state machine. – cHao Nov 05 '11 at 16:05
  • @cHao, I agree that at a fundamental level every useful program is a "state machine." In this context, however, "state machine" refers to a particular high-level construct (as described in the other stackoverflow question). When a C-programmer talks about implementing a state machine, they're usually not referring strictly to the abstract notion of state machines as described in computability theory. – Angelo Nov 06 '11 at 16:42
1

Try this:

#include <stdio.h>

int main(int argc,char *argv[]);
{
        char cmd = '\0';
        char quit = 0;

        while(quit==0) {
                cmd = fgetc(stdin);  

                switch(cmd) {
                        case 'q':
                        {
                          quit =1;
                          break;
                        }
                        // process other cases.
                }
        }
        fprintf(stdout,"Quiting\n");
}
Dipan Mehta
  • 2,110
  • 1
  • 17
  • 31
  • where does q come from in case q: ? – Birey Nov 04 '11 at 14:47
  • it was a typing mistake. it is 'q'. Edit has fixed it. – Dipan Mehta Nov 04 '11 at 14:51
  • q is just 1 char, use string compare to check if "quit" has been typed. – Birey Nov 04 '11 at 14:56
  • @user1027046 then you'll have to elaborate on what you want it to do. – Kevin Nov 04 '11 at 14:57
  • I thought it would be a very simpler principle to extend. Anyway, you can do fgets to acquire complete string first and do string compare. Is there really a tricky situation more than we are discussing here? – Dipan Mehta Nov 04 '11 at 15:08
  • Actually, its a kind of game. where the program open a square grid and I have to give commands to the grid so as to put a cross mark in a specific square. after every command I give to it, it should execute it and ask me for another.. unless I give "quit" command to it.. the program shouldn't terminate – user1027046 Nov 04 '11 at 15:10
  • The above example will do exactly what you want. Just implement the commands other than "q". – Angelo Nov 04 '11 at 15:14
  • 1
    @user1027046 Ok. So i guess i answered that question here. Since you are putting this up as a whole game one wouldn't really want to put the entire game inside the switch case so may be i will be able to run the polling loop in separate thread; but all this is really easy to extend. So I am not quite getting exactly what help you need it with? – Dipan Mehta Nov 04 '11 at 15:18
  • @user1027046 so instead of using the switch, use `strcmp`... `while(!quit) { nextCmd = readCmd(); if(strcmp(nextCmd, "quit") == 0) quit = 1; else handleCmd(nextCmd); }` – Kevin Nov 04 '11 at 15:36
0

If I am not mistaken you can use the following function : system() in stdlib.h where the syntax is as follows:
int system(const char *command);
here you can pass any shell command as a string argument

Vageesh
  • 11
  • 2