Program skips condition which uses argv[]

This is a discussion on Program skips condition which uses argv[] within the C Programming forums, part of the General Programming Boards category; Basically, I'm creating a command line program that performs a Caesar Cypher using the entire ASCII table. Now I can ...

  1. #1
    Unregistered
    Guest

    Program skips condition which uses argv[]

    Basically, I'm creating a command line program that performs a Caesar Cypher using the entire ASCII table. Now I can get it to encrypt, but it doesn't decrypt for some reason.

    I'm not really sure if this conditional statement is correct, so that might be the issue.

    Code:
    if ((argc == 4) && (argv[1] == "-d")){
        i = 1;
        offset = 2;
    
        printf("About to encrypt file...\n");
        inFile = fopen(argv[2], "r");
        outFile = fopen(argv[3], "w");
        printf("The files have been opened.\n");
    
        if (inFile == NULL) {
          printf("Unable to open %s\n", argv[2]);
          exit(1);
        }
    
        if (outFile == NULL) {
          printf("Could not open %s\n", argv[3]);
          exit(1);
        }
    
       while ((current = fgetc(inFile)) != EOF){
          printf("Scanning file %s\n", argv[2]);
          printf("Printing decrypted %c to %s\n", current, argv[3]);
          nRep = (int)current;
          if (nRep - (offset + i) < 0) nRep = (128 + (nRep - (offset + i)));
          else nRep = (nRep - (i + offset));
          current = (char)nRep;
          fprintf(outFile, "%c", current);
          i++;
        }
          fclose(inFile);
          fclose(outFile);
      }
    Now I had a debug statement in my program to display what argv[1] was, and it would show -d. Yet, the encryption function would still get satisfied somehow and it would encrypt the already encrypted file instead.

    Any help would be greatly appreciated

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > argv[1] == "-d")){
    Should be
    strcmp(argv[1],"-d") == 0

  3. #3
    Unregistered
    Guest
    Phew! That fixed it.

    Thank you very much for your help Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21