4

Is it possible to save readline history in a text file and get the history after I reopen my program...

char *line, *expansion;
int result;

stifle_history(7);

while ((line = readline(" $ ")) != NULL) {  

        result = history_expand(line, &expansion);

        rl_bind_key('\t',rl_complete);

        if (result)
                fprintf(stderr, "%s\n", expansion);

        if (result < 0 || result == 2) {
                free(expansion);
                continue;
        }

        strncpy(line, expansion, sizeof (line) - 1);
        free(expansion);
        read_history("history_file");
        write_history("history_file");

        register HIST_ENTRY **the_list;
        register int i;

        if (the_list)
                for (i = 0; the_list[i]; i++)
                        printf("%d: %s\n", i + history_base, the_list[i]->line);

        if (strlen(line) > 0) 
                add_history(line);

I improved my code a bit. Please show the the correct way how I can fix my code to save history in a text file retrieve it afterwards.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
kanoz
  • 43
  • 1
  • 4

1 Answers1

4

According to the manual, you want to look at the write_history and read_history functions.

Kevin Lacquement
  • 5,057
  • 3
  • 25
  • 30
  • 1
    The link is dead, but the documentation is available [here](http://www.delorie.com/gnu/docs/readline/hist_15.html). – user202729 Jan 10 '19 at 02:59
  • The link is dead again, but the documentation is available [here](https://tiswww.case.edu/php/chet/readline/history.html) – hyperbola Apr 16 '21 at 20:40