Thread: Problem with Fork()

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    Here is readCommandLine

    Code:
    char * readCommandLine()
    
    {
      char command[100];
      char *p;
      char *q;
      char *input;
      int i;
      fgets( command, sizeof( command ), stdin );
      if ((p = strchr(command, '\n')) != NULL)
        *p = '\0';
      input = command;
      i=0;
      while (input[i] != '\0')
      {
        history[historyCount][i] = input[i];
        i++;
      }
      history[historyCount][i] = ' ';
      historyCount++;
      return(input); 
    }
    Thank you again

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're returning a pointer to a local variable (which ceases to exist at the point you return)

    Disguising it as
    input = command;
    doesn't help

    This would be a quick fix
    static char command[100];


    A better fix would be to pass the command array as a parameter, then you don't have to worry about allocating it locally
    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. #18
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Also, your history[] array isn't being nul terminated, so you won't be able to use it as a string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    When I delcare command as static it hangs my program..... It appears to hang on the fgets. If I put a printf before it it will print but if I put one after it, it won't get there. Any Ideas?

    here is my edited code:

    Code:
    char * readCommandLine()
    {
      static char command[100];
      char *p;
      char *q;
      char *input;
      int i;
      
      fgets( command, sizeof( command ), stdin );
      
      if ((p = strchr(command, '\n')) != NULL)
        *p = '\0';
      
      //input = command;
      
      i=0;
      while (command[i] != '\0')
      {
        history[historyCount][i] = command[i];
        commandLine[i] = command[i];
        
        i++;
      }
      history[historyCount][i] = ' ';
      historyCount++;
      return command;
    }
    Thank you.

  5. #20
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a working version for you:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char history[100][100];
    int historyCount;
    
    char *readCommandLine(void)
    {
      static char command[100];
      char   *p;
      
      if (fgets( command, sizeof( command ), stdin ))
      {
        if ((p = strchr(command, '\n')) != NULL)
        {
          *p = '\0';
        }
        strcpy(history[historyCount], command);
        historyCount++;
        return command;
      }
      
      return NULL;
    }
    
    int main(void)
    {
      char *cmdLine;
      int i;
      while ((cmdLine = readCommandLine()) != NULL && strcmp(cmdLine, "quit") != 0)
      {
        printf ("You entered command: >%s<\n", cmdLine);
      }
      
      for (i = 0; i < historyCount; i++)
      {
        printf ("Command history %d >%s<\n", i, history[i]);
      }
      
      return 0;
    }
    
    /*
    Output:
    
    this is a command
    You entered command: >this is a command<
    blah blah
    You entered command: >blah blah<
    quit
    Command history 0 >this is a command<
    Command history 1 >blah blah<
    Command history 2 >quit<
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM