Thread: unix cat implementation problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    unix cat implementation problem

    Hello,

    my cat implementation is sort of working but when i put multiple options like -ne, it doesnt work properly.

    it should print something like this

    1 text$
    2 txt$
    3 abc$
    ....

    mine prints like this

    1 text
    2 txt
    3 abc
    text$
    txt$
    abc$

    my main.c

    Code:
    while ((c = getopt (argc, argv, "ne")) != -1) {
                            
                            
                            for(int i = 2; i < argc; i++) {
                                    
                                    file = fopen ( argv[i], "r" );
                                    
                                    if (file == NULL){
                                            
                                            puts ( "\nNo such file or directory\n" );
                                    }
                                    
                                    
                                    
                                    
                            } 
                            
                            
                            //  printf("%c", optopt);
                            
                            switch (c) {
                                            
                                    case 'n':
                                  
                                            optLineNums(argc, argv, file, count, chars);
                                            
                                            break;
                                            
                                            
                                            
                                    case 'e':
                                         
                                            flagE = 1;
                                            optEndOfLine(file, chars, argc, argv);
                                            
                                            
                                            break;
                                            
                                    case '?':
                                            fprintf (stderr, "Unknown option character '-%c'. Use -e or -n options\n", optopt);
                                            break;
                                    default:
                                            abort ();
                            }
                            
                    }

    cat.c

    Code:
    void optLineNums(int argc, char *argv[], FILE *file, int count, char chars) {
            int charCount = 0;
            
            while ( argc ) {
                    
                    
                    chars = fgetc (file);
                    
                    
                    if ( chars == EOF )
                            break;
                    
                    else {
                            if(charCount == 0){
                                    printf("     %i\t",count);
                                    charCount++;
                            }
                            
                            if (chars == '\n'){
                                    count++;
                                    printf("\n"); 
                                    printf("     %i\t",count);   
                                    
                            } else
                                    fputc ( chars, stdout );
                    }
                    
                    
                    
            }
    }
    
    
    
    
    void optEndOfLine(FILE *file, char chars, int argc, char *argv[]) {
            
            
            while ( argc ) {
                    
                    chars = fgetc (file);
                    
                    if (chars == '\n')
                            chars = '$';
                    
                    if ( chars == EOF )
                            break;
                    else
                            fputc ( chars, stdout );
                    
                    if (chars == '$')
                            puts("");
                    
                    
            }
            
    }
    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Why are you opening files in the middle of your getopt loop?

    Typically, your getopt loop would set variables, so that when you actually get around to displaying a file, it would be something like
    displayfile( argv[i], showLineNumbers, showEndOfLineMarkers );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    Why are you opening files in the middle of your getopt loop?

    Typically, your getopt loop would set variables, so that when you actually get around to displaying a file, it would be something like
    displayfile( argv[i], showLineNumbers, showEndOfLineMarkers );

    thank you for the response.. i was opening files in my methods i shared here but I though my problem was coming from opening files twice.. so I decided to put it somewhere else..

    Anyway.. I am not quite sure how your suggestion can be done any easier suggestion would be much better

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well your main would be
    Code:
    int main ( int argc, char *argv[] ) {
      while ( getopt() ) {
        // scan args, and set flags
      }
      for ( i = 1 ; i < argc ; i++ ) {
        // process each arg[i] as a file, 
        // using the options you discovered
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    Well your main would be
    Code:
    int main ( int argc, char *argv[] ) {
      while ( getopt() ) {
        // scan args, and set flags
      }
      for ( i = 1 ; i < argc ; i++ ) {
        // process each arg[i] as a file, 
        // using the options you discovered
      }
    }
    Thanks again for your help..

    Is it something like this?
    Code:
    	      for(int i = 2; i < argc; i++) {
                            
                            file = fopen ( argv[i], "r" );
    		    
    		    
                            if (file == NULL){
                                    
    			  puts ( "\nNo such file or directory\n" );
    		    }
    		    
    		   
                    }
    	      
    	      //for (int x = 1; x<optind; x++){
    		    if (flagN == 1)
    			  optLineNums(argc, argv, file, count, chars);   
    		    if (flagE == 1)
    			  optEndOfLine(file, chars, argc, argv); 
    	      //}
    Last edited by ocg; 08-04-2011 at 05:42 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Something like it, yes.
    But what you're lacking is something for when both flagN and flagE are both set.

    Look at the similarities between optLineNums() and optEndOfLine()
    Both are doing substantially the same thing (print a file to screen).

    If you passed flagN and flagE to a common function, you could display line numbers and/or end of line markers using the SAME code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by ocg View Post
    Code:
        for (int i = 2; i < argc; i++) {
            file = fopen ( argv[i], "r" );
                if (file == NULL) {
                    puts ( "\nNo such file or directory\n" );
                }
            }
    Do you think the above loop will work if options are separated "-n" and "-e" instead of grouped together "-ne"?
    So as suggested, focus on processing the command line options before moving onto processing the input files.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    Something like it, yes.
    But what you're lacking is something for when both flagN and flagE are both set.

    Look at the similarities between optLineNums() and optEndOfLine()
    Both are doing substantially the same thing (print a file to screen).

    If you passed flagN and flagE to a common function, you could display line numbers and/or end of line markers using the SAME code.

    are you saying that I should create a new method for when both flags are set?

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Quote Originally Posted by itCbitC View Post
    Do you think the above loop will work if options are separated "-n" and "-e" instead of grouped together "-ne"?
    So as suggested, focus on processing the command line options before moving onto processing the input files.
    well, that error checking is for testing only... will be fixed...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > are you saying that I should create a new method for when both flags are set?
    You can if you want.
    But don't you also need a function for when no flags are set?

    That's a total of 4 nearly identical functions all doing essentially the same thing (cat) with slightly different output effects.

    Think about this.
    If you added a 3rd flag (say expand tabs to spaces), would you add just one parameter to one function (my way), or copy/paste the 4 functions you have (making 8 functions) just to cover all the possibly permutations of the flags you have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. problem on tree implementation
    By bjte2003 in forum C Programming
    Replies: 1
    Last Post: 02-24-2003, 11:04 PM
  3. C implementation of unix ls command
    By sinkovich in forum Linux Programming
    Replies: 0
    Last Post: 02-24-2003, 04:38 AM
  4. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM
  5. Problem with Huffman Algo implementation...
    By gandhi_rohit in forum C Programming
    Replies: 2
    Last Post: 10-07-2001, 10:34 PM