Thread: Overide for loop condition

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    Overide for loop condition

    Is it possible to overide a for loop condition?

    I need to enter this for loop for(index = optind; index < argc; index++){: and use stdin

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]){
      int optchar;
      int index;
      FILE *fp;
      int lineCount = 1;
      int nflag = 0;
      int sflag = 0;
      int vflag = 0;
      int tflag = 0;
      int c;
      int endline = 1;
      int newlineCount = 0;
      int cntrlFlag = 0;
      int keyboardFlag = 0;
    
      while((optchar = getopt (argc, argv, "nsvt")) != -1){
        switch(optchar){
          case 'n':
                nflag = 1;
            break;
          case 's':
                sflag = 1;
            break;
          case 'v':
                vflag = 1;
            break;
          case 't':
                tflag = 1;
            break;
        }
      }
    
      if(argv[2] == NULL){
        keyboardFlag = 1;
      }
    
      for(index = optind; index < argc; index++){
        if(keyboardFlag == 1){
          fp = stdin;
        }else{
          fp = fopen(argv[index], "r");
        }
          if(fp == NULL){
            fprintf(stdout,"File %s couldn't be opened!\n", argv[index]);
          }
            while((c = fgetc(fp)) != EOF){
              cntrlFlag = 0;
              if(c != '\n'){
                newlineCount = 0;
              }
              if(nflag && endline){
                if(sflag && newlineCount < 2){
                  printf("%5d  ", lineCount);
                  lineCount++;
                }
                if(!sflag){
                  printf("%5d  ", lineCount);
                  lineCount++;
                }
                endline = 0;
              }
              if(iscntrl(c) && tflag && c != '\n'){
                c += 64;
                printf("^%c", c);
                cntrlFlag = 1;
              }
              if(newlineCount < 2 && sflag && !cntrlFlag){
                printf("%c", c);
              }
              if(!sflag && !cntrlFlag){
                printf("%c", c);
              }
              if(c == '\n'){
                endline = 1;
                newlineCount++;
              }
            }
        fclose(fp);
      }
      return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What do you mean "override"? You can reset index inside the loop as much or to whatever you want.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can reset index inside the loop as much or to whatever you want.
    Yes, you can. Be careful doing that however, as it can often lead to unclear, messy code. If you find that becoming a necessity, it's a good sign you might need to refactor your code or something (kind of like with goto).

    In any case, like Dino, I'm not sure what you mean by override. Some other possibility are statements like continue (which terminates execution of the current iteration and restarts the loop in the next iterationg) and break which breaks outside of the current loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. condition variable on read/write locks
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 09:32 AM
  2. Condition variables
    By sethjackson in forum Windows Programming
    Replies: 16
    Last Post: 03-19-2008, 11:42 AM
  3. SDL Condition variables.
    By antex in forum Game Programming
    Replies: 3
    Last Post: 11-11-2005, 07:11 AM
  4. Looping condition
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 02:06 PM
  5. Race condition
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 10-24-2004, 09:42 PM