Thread: Logic approach

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    Logic approach

    I have fixed the -s option to eliminate excess blank lines but still am having some difficulty with parsing multiple command line options eg -ns. Is it possible to do this using this approach that i am trying?

    If i attempt to pass ./prog -ns file.txt in the command line it returns

    Case n!
    File -ns couldn't be opened!
    Segmentation fault

    Rather than listing lines and eliminating excess blank lines.
    When -n file.txt is passed it no longer prints newlines due to my logic problem.

    Code:
       void concatenate_files(char option){
        for(index = optind; index < argc; index++){
          fp = fopen(argv[index], "r");
          if(fp == NULL){
            fprintf(stdout,"File %s couldn't be opened!\n", argv[index]);
          }else{
            printf("File opened!\n");
          }
            printf("%c\n", option);
            int c;
            int aflag = 1;
            int bflag = 1;
            while((c = fgetc(fp)) != EOF){
              if(aflag != 0 && option == 'n'){
                printf("%d  ", lineCount);
                lineCount++;
                aflag = 0;
              }
              if(c == '\n' && bflag != 0 && option == 's'){
                printf("%c", c);
                bflag = 0;
              }
              if(c != '\n'){
                printf("%c", c);
                bflag = 1;
              }
              if(c == '\n'){
                aflag = 1;
              }
            }
          fclose(fp);
        }
      }

    Code:
      while((optchar = getopt (argc, argv, "nsvt")) != -1){
        switch(optchar){
          case 'n':
                printf("Case n!\n");
                concatenate_files('n');
            break;
          case 's':
                 printf("Case s!\n");
                 concatenate_files('s');
            break;
          case 'v':
            break;
          case 't':
            break;
          }
        }
    Last edited by erasm; 08-09-2009 at 09:05 AM. Reason: New code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Handling logic and draw cycles
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 07-25-2009, 10:15 AM
  2. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM