Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单shell C中的History命令_C_Shell - Fatal编程技术网

简单shell C中的History命令

简单shell C中的History命令,c,shell,C,Shell,我试图在shell中创建一个简单的历史函数,但由于某种原因,当我写入历史文本文件时,它会复制输入。例如,用户输入hello,然后输入hi,然后输入bonjour,然后输入history。此历史记录文件如下所示: 你好 你好 你好 你好 你好 你好 你好 你好 你好 历史 . 我的历史文件应该只说你好你好历史 这是我的密码: #include "parser.h" #include "shell.h" #include <stdio.h> #include <string.h&g

我试图在shell中创建一个简单的历史函数,但由于某种原因,当我写入历史文本文件时,它会复制输入。例如,用户输入hello,然后输入hi,然后输入bonjour,然后输入history。此历史记录文件如下所示: 你好 你好 你好 你好 你好 你好 你好 你好 你好 历史 . 我的历史文件应该只说你好你好历史

这是我的密码:

#include "parser.h"
#include "shell.h"
#include <stdio.h>
#include <string.h>
int main(void) {
  char input[MAXINPUTLINE];
  FILE *ptr_file;
  int count=1;
  char *history="history";
  char *last="!!";
  ptr_file =fopen(".simpleshell_history", "w");
  //ptr_file =fopen(".variables", "w");
  signal_c_init();

  printf("Welcome to the sample shell!  You may enter commands here, one\n");
  printf("per line.  When you're finished, press Ctrl+D on a line by\n");
  printf("itself.  I understand basic commands and arguments separated by\n");
  printf("spaces, redirection with < and >, up to two commands joined\n");
  printf("by a pipe, tilde expansion, and background commands with &.\n\n");

  printf("\nlpsh$ ");

  while (fgets(input, sizeof(input), stdin)) {
    stripcrlf(input);
    parse(input);
    //printf(input);
    if(strcmp(input, last)==0) {
        fclose(ptr_file);
        char buf[1000];
        ptr_file =fopen(".simpleshell_history","r");
        while(fgets(buf,1000, ptr_file)!=NULL) {
        }
        memmove(buf, buf+2, strlen(buf));
        printf(buf);

    }
    fprintf(ptr_file,"%i ", count);
    fprintf(ptr_file,"%s\n", input);
    count++;
    if(strcmp(input, history)==0) {
        //printf("it works");
        fclose(ptr_file);
        char buf[1000];
        ptr_file =fopen(".simpleshell_history","r");
    while (fgets(buf,1000, ptr_file)!=NULL) {
            printf("%s",buf);
    }
    fclose(ptr_file);
    ptr_file =fopen(".simpleshell_history","a");
}

    printf("\nlpsh$ ");
  }



  fclose(ptr_file);
  return 0;
}
#包括“parser.h”
#包括“shell.h”
#包括
#包括
内部主(空){
字符输入[MAXINPUTLINE];
文件*ptr_文件;
整数计数=1;
char*history=“history”;
char*last=“!!”;
ptr_file=fopen(“.simpleshell_history”,“w”);
//ptr_file=fopen(“.variables”,“w”);
信号_c_init();
printf(“欢迎使用示例shell!您可以在此处输入命令,一个\n”);
printf(“每行。完成后,按Ctrl+D键在旁边的一行上\n”);
printf(“本身。我理解用\n分隔的基本命令和参数”);
printf(“空格,带<和>,最多连接两个命令的重定向\n”);
printf(“通过管道、平铺扩展和带&.\n\n的后台命令”);
printf(“\nlpsh$”);
while(fgets(input,sizeof(input),stdin)){
stripcrlf(输入);
解析(输入);
//printf(输入);
if(strcmp(输入,最后)==0){
fclose(ptr_文件);
char-buf[1000];
ptr_file=fopen(“.simpleshell_history”,“r”);
while(fgets(buf,1000,ptr_文件)!=NULL){
}
memmove(buf,buf+2,strlen(buf));
printf(buf);
}
fprintf(ptr_文件,“%i”,计数);
fprintf(ptr_文件,“%s\n”,输入);
计数++;
如果(strcmp(输入,历史)==0){
//printf(“它起作用”);
fclose(ptr_文件);
char-buf[1000];
ptr_file=fopen(“.simpleshell_history”,“r”);
while(fgets(buf,1000,ptr_文件)!=NULL){
printf(“%s”,buf);
}
fclose(ptr_文件);
ptr_file=fopen(“.simpleshell_history”,“a”);
}
printf(“\nlpsh$”);
}
fclose(ptr_文件);
返回0;
}

您确定
ptr\u文件
以正确的模式打开了吗?为什么您拒绝检查
fopen
fclose
fprintf
的返回值?您的意思是我确定它已被打开进行写入吗?检查fprintf的返回值是什么意思?我不知道我应该检查返回值。这些函数通常会告诉你它们是否工作/为什么不工作-如果你忽略它,则没有帮助。检查文档/手册页中的返回代码。