Thread: Problem with strcmp()....

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Problem with strcmp()....

    Code:
    #include <stdio.h>
    #include <getopt.h>
    #include <string.h>
    
    void ProcedeWithProg(int pwdYESNO)
    {
       char * string;
       char prompt[100]="";
       char * hostNamePtr = getenv("HOSTNAME");
       char * pwdPtr = getenv("PWD");
       char * colon = ":";
       char exitstring[] = "exit";
       int bytes_read;
       int nbytes = 100;
      
       
       if(!pwdYESNO)
       {
          strcpy(prompt, hostNamePtr);
       }
       else
       {
          strcpy(prompt, hostNamePtr);
          strcat(prompt, colon);
          strcat(prompt, pwdPtr);
       } 
    
       /*Problem is here*/
       do
       {
          printf("%s:> ", prompt);
          string = malloc (nbytes + 1);
          bytes_read = getline (&string, &nbytes, stdin);
       
          if (bytes_read == -1)
          {
             printf("ERROR!\n", prompt);
          }
          else
          {
             printf("You Typed: %s", string);
          }
               
       } while (strcmp(exitstring, string) != 0);
    }
    The loop never ends because strcmp never spits out a 0. My best guess is that it's doing this because string is a pointer. Is this correct? The thing is I can''t get the getline function to work without string being a pointer. Is there a way I could either: Get strcmp to work with string being a pointer, or get getline to work with string not being a pointer? Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It's because the line returned has a newline and that's what always causes the mis-match

    Try strncmp(exitstring, string,4) != 0

    > string = malloc (nbytes + 1);
    You don't free it, this is a memory leak.
    malloc it once outside your do while loop, and free it when the loop exits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    All strings are pointers. Your problem is you didn't read the first paragraph of getline()'s man page

    DESCRIPTION
    getline() reads an entire line, storing the address of the
    buffer containing the text into *lineptr. The buffer is
    null-terminated and includes the newline character, if a
    newline delimiter was found.
    So when you type "exit" the buffer contains "exit\n". strcmp("exit", "exit\n") will never be 0.

    EDIT: Aww, Salem beat me to it.
    Last edited by itsme86; 09-02-2005 at 12:30 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM