Chinaunix

标题: reading configuration file [打印本页]

作者: longtem    时间: 2008-11-24 15:37
标题: reading configuration file
This is a example code for reading the configuration file.
You may need to modify the code according to your case.
               
               
                #include stdio.h>
#include string.h>
#define BUFLEN 100
int main(int argc, char** argv)
{
    FILE* file = NULL;
    char buf[BUFLEN];
    if ((file = fopen(argv[1], "r")) == NULL)
    {
        perror("Fail to open file");
        return -1;
    }
    while (!feof(file))
    {
        fscanf(file, "%*[/s/t\n]s");/*skip spaces and tabs*/
        if ((buf[0] = fgetc(file)) == EOF)
            break;/*reaching the end of the file*/
        if (buf[0] == '#')/*this line is just a comment*/
        {
            fscanf(file, "%*[^\n]s");/*skip the comment*/
            fgetc(file);/*skip the newline*/
            continue;
        }
        else
        {
            bzero(buf + 1, BUFLEN-1);
            NOTE1: fscanf(file, "%99[^\n#]s", buf + 1);/*scan the content*/
            fscanf(file, "%*[^\n]s");/*skip the comment*/
            fgetc(file);/*skip the newline*/
        }
        /*do the configuring work*/
        printf("Get: %s.\n", buf);
    }
    return 0;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/18481/showart_1662974.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2