Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从文件中读取文本行_C_Fgets - Fatal编程技术网

C 从文件中读取文本行

C 从文件中读取文本行,c,fgets,C,Fgets,通用条款4.5.1 c89 我使用以下代码从配置文件中读取一行文本。配置文件目前很小,将随着要添加的新字段而增长。我几乎可以自己设计配置文件的样子。因此,我采取了以下方法: config.cfg # Configuration file to be loaded # protocol to use either ISDN, SIP, protocol: ISDN # run application in the following mode, makecall or waitcall m

通用条款4.5.1 c89

我使用以下代码从配置文件中读取一行文本。配置文件目前很小,将随着要添加的新字段而增长。我几乎可以自己设计配置文件的样子。因此,我采取了以下方法:

config.cfg

# Configuration file to be loaded 

# protocol to use either ISDN, SIP, 
protocol: ISDN

# run application in the following mode, makecall or waitcall
mode: makecall
我使用冒号作为搜索所需配置类型的一种方式。我只是想知道有没有更好的办法

我使用的代码如下:

static int load_config(FILE *fp)
{
    char line_read[LINE_SIZE] = {0};
    char *type = NULL;
    char field[LINE_SIZE] = {0};
    char *carriage_return = NULL;

    /* Read each line */
    while(fgets(line_read, LINE_SIZE, fp) != NULL) {
        if(line_read != NULL) {
            /* Ignore any hashs and blank lines */
            if((line_read[0] != '#') && (strlen(line_read) > 1)) {
                /* I don't like the carriage return, so remove it. */
                carriage_return = strrchr(line_read, '\n');
                if(carriage_return != NULL) {
                    /* carriage return found so relace with a null */
                    *carriage_return = '\0';
                }

                /* Parse line_read to extract the field name i.e. protocol, mode, etc */
                parse_string(field, line_read);
                if(field != NULL) {
                    type = strchr(line_read, ':');
                    type+=2; /* Point to the first character after the space */

                    if(strcmp("protocol", field) == 0) {
                        /* Check the protocol type */
                        printf("protocol [ %s ]\n", type);
                    }
                    else if (strcmp("mode", field) == 0) {
                        /* Check the mode type */
                        printf("mode [ %s ]\n", type);
                    }
                }
            }
        }
    }

    return TRUE;
}

/* Extract the field name for the read in line from the configuration file. */
static void parse_string(char *dest, const char *string)
{
    /* Copy string up to the colon to determine configuration type */
    while(*string != ':') {
        *dest++ = *string++;
    }
    /* Insert nul terminator */
    *dest = '\0';
}
是你的朋友

是你的朋友


如果您能设计配置文件的外观,我会选择XML,用XML解析它。这很轻松。

如果您能设计配置文件的外观,我会选择XML,用XML解析它。简单解析问题的标准答案是使用lex和yacc

但是,由于您可以自由设置配置文件的形式,因此应该使用实现各种配置文件格式的众多库中的一个,并且只使用该库


例如,它似乎充分满足了您的需求,但也可以看看其他可能更简单的选项。

简单解析问题的标准答案是使用lex和yacc

但是,由于您可以自由设置配置文件的形式,因此应该使用实现各种配置文件格式的众多库中的一个,并且只使用该库

例如,它似乎能充分满足您的需求,但也可以看看其他可能更简单的选项。

更简单的应该是:

static int load_config(FILE *fp)
{
  int r=0;
  char line[LINE_SIZE], field[LINE_SIZE], type[LINE_SIZE], dummy[LINE_SIZE];

  /* Read each line */
  while( fgets(line, LINE_SIZE, fp) )
  {
    if( strchr(line,'\n') ) *strchr(line,'\n')=0;
    if( 3==sscanf(line,"%[^: ]%[: ]%s,field,dummy,type) )
      ++r,printf("\n %s [ %s ]",field,type);
  }
  return r;
}
应该是:

static int load_config(FILE *fp)
{
  int r=0;
  char line[LINE_SIZE], field[LINE_SIZE], type[LINE_SIZE], dummy[LINE_SIZE];

  /* Read each line */
  while( fgets(line, LINE_SIZE, fp) )
  {
    if( strchr(line,'\n') ) *strchr(line,'\n')=0;
    if( 3==sscanf(line,"%[^: ]%[: ]%s,field,dummy,type) )
      ++r,printf("\n %s [ %s ]",field,type);
  }
  return r;
}

谢谢,我来看看。同时,你可以看到我的源代码中潜在的问题。问题是C89,lex,yacc,。。。不是这个的一部分。谢谢,我来看看。同时,你可以看到我的源代码中潜在的问题。问题是C89,lex,yacc,。。。不是这个问题的一部分。问题是C89,正则表达式不是这个问题的一部分。问题是C89,正则表达式不是这个问题的一部分。