简体   繁体   English

从文件中读取文本行

[英]reading in lines of text from file

gcc 4.5.1 c89 gcc 4.5.1 c89

I am using the following code to read in a line of text from a configuration file. 我正在使用以下代码从配置文件中读取一行文本。 The configuration file is small at the moment, will grow with new fields to add. 目前配置文件很小,将随着新字段的添加而增长。 And I can pretty much design what the configuration file will look like myself. 我几乎可以设计配置文件的外观。 So I have done it in the following way: 因此,我通过以下方式完成了此操作:

config.cfg 的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

I have used the colon as a way to search for the type of configuration that will be needed. 我已使用冒号作为搜索所需配置类型的方法。 I am just wondering if there a better way to do this? 我只是想知道是否有更好的方法可以做到这一点?

My code I have used is as follows: 我使用的代码如下:

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';
}

If you can design what the configuration file will look like i'd go for XML, parsing it with Expat . 如果可以设计配置文件的外观,那么我将使用XML,使用Expat对其进行解析。 It's painless. 没痛苦

The standard answer to simple parsing problem is to use lex and yacc. 简单解析问题的标准答案是使用lex和yacc。

However, as you have freedom to set the form of the configuration file, you should use one of the numerous libraries that implement various configuration file formats, and just use that one. 但是,由于您可以自由设置配置文件的格式,因此应使用实现各种配置文件格式的众多库之一,而只需使用其中一种即可。

http://www.google.com/search?q=configuration+file+parser http://www.google.com/search?q=configuration+file+parser

http://www.nongnu.org/confuse/ for instance, would seem to cover your needs adequately, but take a look at various other options that might be simpler as well. 例如, http: //www.nongnu.org/confuse/似乎可以充分满足您的需求,但请看一下其他各种可能更简单的选择。

easier should be: 更容易的应该是:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM