小编kan*_*noz的帖子

如何将读取历史记录保存在文本文件中并在之后调用

是否可以将读取行历史记录保存在文本文件中并在重新打开程序后获取历史记录...

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);
Run Code Online (Sandbox Code Playgroud)

我改进了我的代码。请展示正确的方法,如何修复我的代码以将历史记录保存在文本文件中,然后检索它。

c history readline

5
推荐指数
1
解决办法
4027
查看次数

标签 统计

c ×1

history ×1

readline ×1