Thread: IF problem

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    IF problem

    It's intended to be a coder/decoder.

    The program seems to jump over if(argv[1]=="c") and
    if(argv[1]=="c") even if argv[1] is c or d, why??
    If I write argv[1]=='c' the compiler says there's an error, so how I have to write argv[1]=="c", which a string, is that the error?

    #include "stdio.h"
    #include "stdlib.h"

    main (int argc,char *argv[])
    {
    FILE *from,*to;
    char ch,a;

    if(argc!=4){
    printf("USO: <c|d> <archivo> <archivosalida>");
    exit(1);
    }
    if((from=fopen(argv[2],"rb"))==NULL){
    printf("Error al abrir %s",argv[2]);
    exit(1);
    }
    if((to=fopen(argv[3],"wb"))==NULL){
    printf("Error al abrir %s",argv[3]);
    exit(1);
    }

    if(argv[1]=="c"){
    while(!feof(from)){
    ch=fgetc(from);
    a=ch-1;
    fputc(a,to);
    }
    printf("Archivo codificado!!");
    }

    if(argv[1]=="d"){
    while(!feof(from)){
    ch=fgetc(from);
    a=ch-1;
    fputc(a,to);
    }
    printf("Archivo decodificado!!");
    }

    fclose(from);
    fclose(to);
    printf("Gracias por usar software Xuaco!!");
    }
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > #include "stdio.h"
    > #include "stdlib.h"

    Use <> for library files
    #include <stdio.h>

    > if(argv[1]=="c")
    Well you have 2 choices

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

    or

    if ( argv[1][0] == 'c' )

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    SO?

    So why is if(argv[1]="c") erroneous?????
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Improving it.

    In order to improve it, how could I make that the program codecs or decodecs the specified file(argv[2]) and puts it right in that file and not in another file(the one specified in argv[3])???
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Well, firstly you have written this:
    Code:
    if(argv[1]="c")
    You are trying to assign "c" to the array argv, rather than trying to test for equality. The difference is in the "=" operator. You should be using "==".

    Secondly you can't test for equality with strings using the "==" operator anyway, so try using a function that is designed for that purpose. Such as strcmp() as salem pointed out earlier.

    Otherwise you could use salems second suggestion which is to test to see whether the first character of the second argument is equal to the character 'c'. Notice the use of '' rather than "".

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > how could I make that the program codecs or decodecs the specified file(argv[2])
    Well you would decode to a temporary file
    Then delete the original file, and rename the temp file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM