如何将读取历史记录保存在文本文件中并在之后调用
是否可以将读取行历史记录保存在文本文件中,并在重新打开程序后获取历史记录...
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);
我改进了我的代码。请显示正确的方法,我如何修复我的代码以将历史记录保存在文本文件中,然后检索它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据手册,你想看看
write_history
和read_history
函数。According to the manual, you want to look at the
write_history
andread_history
functions.