0

Can someone please tell me why this code will not work? It does compile. When I type decrypt as the argv[1] argument in the command line, it still gives me the else output. i.e. argv[1] is not satisfied even though it should be. This is a work in progress so ignore the other code

 if ((argv[1] == "decrypt"))
      { 
      printf("Decrypting...\n");
        c = getc(fp1);
        if (c != EOF)
          { 
          fread(inputbuffer, sizeof(char), 50 , fp1);   
            printf("%s", inputbuffer);
            /*while(inputbuffer[i]!=EOF)
            {
            fputc((inputbuffer[i] / 2) - 5, fp2);
            }*/
          } 
      }

      else {printf("argv not working");}
adohertyd
  • 2,689
  • 19
  • 58
  • 78

1 Answers1

11

You need to use strcmp() to compare strings:

if ((strcmp(argv[1], "decrypt") == 0)

More detail:

What you are comparing are the two memory addresses for the different strings, which are stored in different locations. Doing so essentially looks like this:

if(0x00403064 == 0x002D316A) // Two memory locations
{
    printf("Yes, equal");
}
Community
  • 1
  • 1
  • I've changed it to strcmp and now the program crashes! It's not printing "Decrypting..." so its still the argv causing the problem EDIT: fixed it my bad your solution was perfect thank you – adohertyd Dec 22 '11 at 15:42
  • @adohertyd: Your code has other bugs in it, such as the possible non-null terminated string you're trying to print. Also make sure `argc` is at least 2. –  Dec 22 '11 at 15:43