Thread: problem with compare two characters

  1. #1
    Registered User
    Join Date
    Mar 2009

    problem with compare two characters

    I Have main function:
    int main(int argc, char *argv[]) {
      int count = 0;
      FILE *file_content;
      int ch;
      file_content = fopen(argv[1], "r");
      while ((ch = fgetc(file_content)) != EOF) {
        if ( ch == argv[2] ) {
      printf("Characters found %d\n", count);
      return (0);
    where first argument is path to text file and second argument is character
    for example ./program myFile.txt a

    Problem is with comparing these two characters from second argument and character get from reading file. Variable ch must be int but in comparing I compare integer with pointer. How to convert this to same data type as second argument which is pointer to char?

    I tried strcmp function too but same result.

    Thanks for help.

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    argv[2] is not a character, but a pointer. You should do *argv[2]

    You should also check the argc before accessing to argv[1] or argv[2].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop with multiple characters
    By arctic_blizzard in forum C Programming
    Replies: 16
    Last Post: 09-20-2008, 12:25 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  5. string compare problem
    By gandalf_bar in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2004, 11:11 AM