开发者

How to save readline history in a text file and call it afterwards

开发者 https://www.devze.com 2023-04-02 03:44 出处:网络
Is it possible to save readline history in a text file and get the history after I reopen my program...

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号