将char *添加到char **

时间:2018-10-01 03:11:47

标签: c

我试图在c中创建一个shell,并试图将用户输入的内容放入字符串数组(char **)。我的问题是,例如,如果用户输入“ pwd”,然后在下一个循环中输入“ ls”,则历史记录中的两个元素都将变为ls。任何帮助将不胜感激。

 char **history = calloc(MAXARGS, sizeof(char*)); //MAXARGS = 128
 int histCounter = 0;
 char *commandline = calloc(MAX_CANON, sizeof(char));

 while(go) {  //exits loop when you type exit

    if (fgets(buffer, BUFFERSIZE, stdin) != NULL) {
              len = (int) strlen(buffer);
        buffer[len-1] = '\0';   
        strcpy(commandline, buffer);
    }

  history[histCounter] = commandline;

  if(histCounter > 0) {    //just to see if they are different
        printf("%s\n", history[histCounter-1]);
        printf("%s\n", history[histCounter]);
    }   
  histCounter = histCounter + 1;
  }

1 个答案:

答案 0 :(得分:0)

尝试类似的东西:

var prompt = require('prompt');

  //
  // Start the prompt
  //
  prompt.start();

  //
  // Get two properties from the user: username and email
  //
  prompt.get(['username', 'email'], function (err, result) {
    //
    // Log the results.
    //
    console.log('Command-line input received:');
    console.log('  username: ' + result.username);
    console.log('  email: ' + result.email);
  });

或者更好的是,查看链接列表并改为使用它。