天地维杰网

人如秋鸿来有信,事若春梦了无痕


  • 首页

  • Redis

  • java

  • linux

  • 日常问题

  • Spring和Springboot

  • Mac相关

  • 中间件

  • 架构

  • python

  • 前端

  • jvm

  • c语言

  • web3

  • 归档

  • 关于

  • 搜索
close

c语言终止 fget stdin 输入流

时间: 2022-10-16   |   分类: redis   c     |   原创   |   阅读: 636 字 ~2分钟

在阅读Redis源码,有一段是从配置文件、输入流和选项参数中读取配置内容,在调试输入流这一步,虽然能输入,但是我却不知道怎么停止输入流。尝试过使用ctrl + c ,但会使整个程序都终止。

void loadServerConfig(char *filename, char config_from_stdin, char *options) {
    //新建空sds对象 config ,最后这个config还会被手动释放的
    sds config = sdsempty();
    char buf[CONFIG_MAX_LINE+1];
    FILE *fp;

    /* Load the file content */
    if (filename) {
        if ((fp = fopen(filename,"r")) == NULL) {
            serverLog(LL_WARNING,
                    "Fatal error, can't open config file '%s': %s",
                    filename, strerror(errno));
            exit(1);
        }
        while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL)
            config = sdscat(config,buf);
        fclose(fp);
    }
    /* Append content from stdin */
    if (config_from_stdin) {
        serverLog(LL_WARNING,"Reading config from stdin");
        fp = stdin;
        while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL){
            config = sdscat(config,buf);
            printf("========>loadServerConfig=>stdin:%s\n", config);
        }
    }

    /* Append the additional options */
    if (options) {
        config = sdscat(config,"\n");
        config = sdscat(config,options);
    }
    //从配置文本中中解析配置
    loadServerConfigFromString(config);
    sdsfree(config);
}

有可能是查找的关键字不对,在搜索引擎上也没找正确的答案,后来通过使用how to stop fget s stdin关键词在bing国际版搜到一篇文章,有一个评论是这么回答的,I have to hit Ctrl-D or fill up the buffer to make it stop reading.意思就是使用ctrl + D快捷键或者将缓冲区填满。然后我尝试使用ctrl + d ,确实终止了输入流并正常进入下一步的配位解析的步骤。

问题解决。

参考文档:https://www.daniweb.com/programming/software-development/threads/510458/c-programming-stop-reading-from-stdin

后来翻书 << c primer plus >>的第507页其实是有讲终止键盘输入的

每次按下Enter键,系统便会处理缓冲区中储存的字符,并在下一行打 印该输入行的副本。这个过程一直持续到以UNIX风格模拟文件结尾(按下Ctrl+D)。在PC中,要按下Ctrl+Z。
#redis# #c# #输入流# #源码#
Redis 客户端处理
Pip配置多个源
  • 文章目录
  • 站点概览
不与天斗Domino

不与天斗Domino

Programmer & Architect

183 日志
15 分类
224 标签
© 2013 - 2023 天地维杰网 京ICP备13019191号-1
Powered by - Hugo v0.63.2
Theme by - NexT
0%