Why doesn't this work?

This is a discussion on Why doesn't this work? within the C Programming forums, part of the General Programming Boards category; When I run this, the 1st print statement prints out a >, yet the if gets evaluated to false and ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    19

    Why doesn't this work?

    When I run this, the 1st print statement prints out a >, yet the if gets evaluated to false and the inside print does not execute. wtf?

    Code:
    printf(command.argv[argc1 - 2]); 
    
    if(command.argv[argc1 - 2] == '>')
    	   {
    	    printf("blah");
    	   }
    edit: I tried single and double quotes around the >

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Perhaps a little more code that we can compile and fiddle with would help a bit. Because
    if(command.argv[argc1 - 2] == '>')

    just doesn't look right to me.

    >printf(command.argv[argc1 - 2]);
    This isn't right, if I'm correct and you're using names for your structure members that are already identified with being arguments to main, and you're trying to print a single character, it should be like this:
    printf ( "%c", command.argv[argc1 - 2] );

    If you're working with the arguments to main then you're way off.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    19
    Actually, I'm not using args to main. It's a simple shell program , and command is a struct that holds the parsed command line that was entered by the user. I changed it a little bit, so I use this for loop to examine the contents of the argv array:

    Code:
    for(k = 0; command.argv[k] != NULL; k++)   	printf("Arg %i : %s\n", k, command.argv[k]);
    Then here is the full if statement:

    Code:
     for(k = 0; command.argv[k] != NULL; k++) 
    	  if(command.argv[k] == '>')
    	   {
    	    printf("DFSDF");
    	    redirectOut = TRUE;
    	    command.argv[k] = '\0';
    	    filename = command.argv[k+1];
    	    command.argv[k+1] = '\0';
    	   }
    The for loop outputs this:
    Arg0: ls
    Arg1: >
    Arg2: textfile

    Yet the if statement is never entered. I am befuddled.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if(command.argv[k] == '>')
    This won't work because argv[k] is a string. Strings cannot be compared in C with the == operator, you have to use strcmp.
    Code:
    if ( strcmp ( command.argv[k], ">" ) == 0 )
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    19
    Doh, I feel stupid now. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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