gpt4 book ai didi

c - shell 中的历史功能

转载 作者:行者123 更新时间:2023-11-30 17:44:43 24 4
gpt4 key购买 nike

我必须实现 hist 命令,包括 !k 和 !!

2个函数:

void addInHistory(char **history,char *command,int *list_size,int history_capacity)
{
int index=*(list_size);
if(command[0]!='\n')
{
if(index==history_capacity-1)
{
printf("History is full.Deleting commands.");
}
else
{
char current_command[COMMAND_SIZE];
strcpy(current_command,command);
history[index++]=current_command;
}
}
}
void printHistory(char **history,int size)
{
int i;
for(int i=0;i<=size;i++)
{
printf("%d. %s\n",i+1,history[i]);
}
}

如有任何帮助,我们将不胜感激。

最佳答案

对于 C 解决方案

 char current_command[COMMAND_SIZE];
strcpy(current_command,command);
history[index++]=current_command;

应该是

history[index++]= strdup(command);       

使用完毕后请务必将其释放。

关于c - shell 中的历史功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19878371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com