Thread: Help please

  1. #16
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    here's what I have now, it seems to work, but it might only appear that way. I need to make a file of all 0's to check it.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    main (int argc, char** argv )
    {   int Key = atoi(argv[2]);
    FILE *in, *out;
    int ch = *argv[1];
    
    if(Key > 255 || Key < 0)
    {
     fprintf(stderr, "key must be between 0 and 255");
     return(EXIT_FAILURE);
    }
    if ( argc != 5 || (*argv[1] != 'e' && *argv[1] != 'd' && *argv[1] != 'E' && *argv[1] != 'D') )
    {
    fprintf( stderr, "only enter e to encipher or d to decipher\n");
    return( EXIT_FAILURE );
    }
    /* Open the source file for "read binary" so that */
    /* end of line characters are not translated */
    if ( (in = fopen( argv[3], "rb")) == NULL )
    {
    perror( argv[3] );
    return( EXIT_FAILURE );
    }
    /* Open the destination file for "write binary" */
    if ( (out = fopen( argv[4], "wb")) == NULL )
    {
    perror( argv[4] );
    return( EXIT_FAILURE );
    }
    /* Copy characters from input to output until end of file */
    while ( (ch = fgetc(in)) != EOF )
    {
     if(strcmp(argv[1], "e") == 0 || strcmp(argv[1], "E") == 0)
     {
     ch = (ch + Key) % 256;
     fputc( ch, out );
     Key++ % 256;
     }
     if(strcmp(argv[1], "d") == 0 || strcmp(argv[1], "D") == 0)
     {
     ch = (ch - Key) % 256;
     fputc( ch, out );
     Key++ % 256;
     }
    }
    /* Close the input and the output files */
    if ( fclose( in ) == EOF )
    {
    perror( argv[1] );
    return( EXIT_FAILURE );
    }
    if ( fclose( out ) == EOF )
    {
    perror( argv[2] );
    return( EXIT_FAILURE );
    }
    return( EXIT_SUCCESS );
    }

  2. #17
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Its easier for us to (manually) check your code if you use whitespace and indent your code. Some people (including myself) wont look through the code if it looks like this, because its too easy to miss important details when its not formatted "properly".

  3. #18
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    sorry, I think I've made it better now:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    main (int argc, char** argv )
    {   
     int Key = atoi(argv[2]);
     FILE *in, *out;
     int ch = *argv[1];
    
      if(Key > 255 || Key < 0)
      {
       fprintf(stderr, "key must be between 0 and 255");
       return(EXIT_FAILURE);
      }
    
      if ( argc != 5 || (*argv[1] != 'e' && *argv[1] != 'd' && *argv[1] != 'E' && *argv[1] != 'D') )
      {
       fprintf( stderr, "only enter e to encipher or d to decipher\n");
       return( EXIT_FAILURE );
      }
    
      if ( (in = fopen( argv[3], "rb")) == NULL )
      {
        perror( argv[3] );
        return( EXIT_FAILURE );
      }
    
      if ( (out = fopen( argv[4], "wb")) == NULL )
      {
        perror( argv[4] );
        return( EXIT_FAILURE );
      }
    
     while ( (ch = fgetc(in)) != EOF )
     {
       if(strcmp(argv[1], "e") == 0 || strcmp(argv[1], "E") == 0)
       {
         ch = (ch + Key) % 256;
         fputc( ch, out );
         Key++ % 256;
       }
    
       if(strcmp(argv[1], "d") == 0 || strcmp(argv[1], "D") == 0)
       {
         ch = (ch - Key) % 256;
         fputc( ch, out );
         Key++ % 256;
       }
     }
    
     if ( fclose( in ) == EOF )
     {
      perror( argv[1] );
      return( EXIT_FAILURE );
     }
    
     if ( fclose( out ) == EOF )
     {
       perror( argv[2] );
       return( EXIT_FAILURE );
     }
    
    return( EXIT_SUCCESS );
    }

  4. #19
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    A quick look and it seems ok. However, there are still many places where segfaults would occur if the number of arguments given isnt the exact number you are expecting (5).

    For example, if no arguments were passed, the following lines of could would produce an error (segfault), other lines of code do similar things for the specific indices, but Ive only included the first error for each index
    Code:
     int Key = atoi(argv[2]);
    //...
     int ch = *argv[1];
    // ...
    if ( (in = fopen( argv[3], "rb")) == NULL )
    //...
    if ( (out = fopen( argv[4], "wb")) == NULL )
    Also, "main" should be "int main"

    EDIT: What are you trying to do here?
    Code:
         Key++ % 256;
    This does the same as
    Code:
         Key++;
    Last edited by nadroj; 11-04-2009 at 08:21 PM.

Popular pages Recent additions subscribe to a feed