Thread: Help please

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    Help please

    Here's what I'm supposed to do:
    Write a program (in C), which enciphers or deciphers an input file.
    Assume that the input file is any type of file, at all. So you must read it in
    using binary mode. Also, the output file will be written using binary mode.
    The program will encipher the input file by doing the following:
    1. Initialize N to the key value from the command line.
    2. While not EOF:
    a. Read in a byte, B, of the file.
    b. Set B to (B+N) mod 256.
    c. Write out B.
    d. Increment N mod 256.
    Notice that this is the familiar “filter” type of program. A file is deciphered by following exactly the
    same procedure, but the key is subtracted from the byte in step c.
    Details: The key will be a value from 0 to 255. If it is not, write out an error message and exit. Use
    command line parameters for the seed and for the file names.
    C:\> cipher E 221 myImage.jpg myCipheredImage.bin
    C:\> cipher D -221 myCipheredImage.bin originalImage.jpg
    Compute (B+N) mod 256 by doing ordinary addition (B+N) and then doing modular division % 256.
    So numbers will always wrap around into the range 0..255. Compute (B-N) mod 256 by computing
    (B+256-N) % 256. Test thoroughly that this part of your program is working.
    Expect that input files can be anything at all, including ordinary text files, MS word files, image files, or
    executables. Of course, the result after deciphering should be identical to the original file. Use
    strcmp() to compare the first parameter argv[1] with “E” or “D”. Use atoi(argv[2]) for
    converting the second parameter into an int. You will need to use fopen(), fgetc(),
    fputc(),and fclose().
    Although this cipher will leave your files unreadable by most casual interlopers, it is still what is called
    a “simple cipher.” It is nowhere near commercial quality and would easily be broken by any
    cryptanalyst.
    Debugging: Create some test files and use them to test the operation of your program. The most
    obvious test file is a file of all zero-bytes. The output of your program with this file should be an
    ascending sequence of bytes, mode 256. The test file can be created by a program you write. Inspect
    the output of your program using Irfanview or some other hex dump program. Also test your program
    on some image files and some other files.

    Here's what I have:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    /* File copy -- copy a source to a destination.
    If the destination file exists it will be copied over.
    */
    int main ( int argc, char** argv )
    {  int N;
       printf("enter the key value(0 to 255)");
       scanf("%d", &N);
       if(N>255 || N<0)
       exit(EXIT_FAILURE);
    FILE *in, *out;
    int ch;
    if ( argc != 'E' || argc != 'e' || argc != 'D' || argc != 'E' )
    {
    fprintf( stderr, "copy source dest\n");
    return( EXIT_FAILURE );
    }
    /* Open the source file for "read binary" so that */
    /* end of line characters are not translated */
    if ( (in = fopen( argv[1], "rb")) == NULL )
    {
    perror( argv[1] );
    return( EXIT_FAILURE );
    }
    /* Open the destination file for "write binary" */
    if ( (out = fopen( argv[2], "wb")) == NULL )
    {
    perror( argv[2] );
    return( EXIT_FAILURE );
    }
    /* Copy characters from input to output until end of file */
    while ( (ch = fgetc(in)) != EOF )
    {
     if(argc == 'e' || argc == 'E')
     {
     ch = (ch + N) % 256;
     fputc( ch, out );
     N++ % 256;
     }
     if(argc == 'D' || argc == 'd')
     {
     ch = (ch - N) % 256;
     fputc( ch, out );
     N++ % 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 );
    }
    what am I doing wrong?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Beats me. Whats the problem? Compiler error? Logic/runtime error? Describe your problem so we know where to start (opposed to reading that entire block of your assignment, reading and understanding your code, finding the problem, fixing the problem).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Sorry, I was about to leave when i posted it, right now the problem is that it just prints "copy source dest"

  4. #4
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Your line :
    Code:
    if ( argc != 'E' || argc != 'e' || argc != 'D' || argc != 'E' )
    is checking 'E' twice. Should it be checking some other value??

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by kcpilot View Post
    Your line :
    Code:
    if ( argc != 'E' || argc != 'e' || argc != 'D' || argc != 'E' )
    is checking 'E' twice. Should it be checking some other value??
    First of all argc can never equal 'e' 'E' 'd' or 'D'. Now do you see the reason why the program is only printing "copy source dest"

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by itCbitC View Post
    First of all argc can never equal 'e' 'E' 'd' or 'D'. Now do you see the reason why the program is only printing "copy source dest"
    Sure it could -- Though, why on earth someone would want 101 command line arguments is beyond me.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    ok, I get that that part is what is wrong, but what would I do to make it check if what they type for that argument E, e, D, or d?

    edit: can I make the parameters of main whatever I want? so there would be four total. one would be an int that's the key, one would be a char that should be e or d, and then the name of the input file and the output file?
    Last edited by DLH112; 11-04-2009 at 03:24 PM.

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    In truth, all are strings. You'll have to convert the input to whatever you want. Usually, this is done via switches (if you have multiple data types).

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    argc is the number of arguments, argv is an array of pointers to chars(or a pointer to pointer to char, if you prefer).
    argv[0] = name of your program(not exactly, it's probably better to say that it's the command used to invoke your program)
    argv[1] = first argument on the command line
    argv[2] = second argument on the command line
    ...
    You obviously need to check if argc is > 1 though.
    Last edited by MisterIO; 11-04-2009 at 03:52 PM.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    wow, i just realized a huge flaw in my logic, the if statement should look something like this
    if ( argc != 5 || (*argv[1] != 'e' && *argv[1] != 'd') )

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    if ( argc != 5 || (*argv[1] != 'e' && *argv[1] != 'd') )
    If you run the program with no arguments, youll get a segfault. As previously mentioned:
    Quote Originally Posted by MisterIO
    You obviously need to check if argc is > 1 though.
    You need to make sure that (at least) one argument is given, i.e. argc > 1 (argv[0] is the current filename, argv[1] is the first argument, etc).

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by nadroj View Post
    Code:
    if ( argc != 5 || (*argv[1] != 'e' && *argv[1] != 'd') )
    If you run the program with no arguments, youll get a segfault. As previously mentioned:You need to make sure that (at least) one argument is given, i.e. argc > 1 (argv[0] is the current filename, argv[1] is the first argument, etc).
    You didn't actually read my post.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I didnt? I was citing you as part of the solution to the op's (latest) problem). I quoted what you said, and re-stated it (equivalently).

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by nadroj View Post
    I didnt? I was citing you as part of the solution to the op's (latest) problem). I quoted what you said, and re-stated it (equivalently).
    Yes, I misunderstood, sorry.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Kennedy View Post
    Sure it could -- Though, why on earth someone would want 101 command line arguments is beyond me.
    Yep! that would make "complete sense" when writing obfuscated C code.

Popular pages Recent additions subscribe to a feed