Read and write configuration files

Method and system configuration file read and write test function

 

#include"readConfig.h"
#define CFGNAME "./test.txt"

void mymenu()
{
    printf("=========================\n");
    printf("1 Test Write Config file\n");
    printf("2 Test Read Config file\n");
    printf("0 exit\n");
    printf("=========================\n");
}

int TGetCfg()
{
    int ret = 0;
    char name[1024] = {0};
    char value[1024] = {0};
    int vlen = 0;

    printf("\nplease input key: ");
    scanf("%s,",name);

    ret = GetCfgItem(CFGNAME, name, value, &vlen);
    if (0 != ret)
    {
        printf("func WriteCfgItem err:%d \n",ret);
        return ret;
    }

    printf("value:%s \n",value);
    return ret;
}

int TWriteCfg()
{
    int ret = 0;
    char name[1024] = {0};
    char value[1024] = {0};

    printf("\nplease input key: ");
    scanf("%s",name);

    printf("\nplease input value: ");
    scanf("%s", value);

    ret = WriteCfgItem(CFGNAME, name, value, strlen(value));
    if (0 != ret)
    {
        printf("func WriteCfgItem err : %d \n", ret);
        return ret;
    }

    printf("your input name is %s , value is %s\n", name, value);
    return ret;
}


int main ()
{
    int choice;

    for(;;)
    {
        mymenu();
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                TWriteCfg ();
                break;
            case 2:
                TGetCfg();
                break;
            case 0:
                exit(0);
            default:
                exit(0);
        }
    }

    printf("end......");
    return 0;

}

 

/*
readConfig.h
*/
#ifndef _READCONFIG_H__
#define _READCONFIG_H__

#include<stdlib.h>
#include<string.h>
#include<stdio.h>

#define MaxLine 2048

#ifdef __cplusplus
extern "C"{
#endif

int GetCfgItem(char *pFileName, char *pKey, char *pValue, int *pValueLen);

// function declaration statement just passed parameter types, parameter names can be achieved when different 
int WriteCfgItem ( char * pFileName, char * pItemName, char * pItemValue, int itemValueLen);

#ifdef __cplusplus
}

#endif

#endif

 

/*
readConfig.c
*/
#include "readConfig.h"

// get the configuration item 
int GetCfgItem ( char * pFileName, char * pKey, char * pValue, int * pValueLen)
{
    an int Ret = 0 ;
    FILE *fp = NULL;
    char *pTmp = NULL, *pEnd = NULL, *pBegin = NULL;

    char lineBuf [Maxline];

    fp = fopen(pFileName, "r");
    if (fp == NULL)
    {
        //-1 means the file is not exist
        ret = -1;
        return ret;
    }

    while (!feof(fp))
    {
        memset (lineBuf, 0 , sizeof (lineBuf));
        fgets (lineBuf, Maxline, fp);
        
        PTMP = the strchr (lineBuf, ' = ' );     // do not skip line number = 
        IF (PTMP == NULL)
        {
            continue;
        }
        
        PTMP = Strstr (lineBuf, pKey);     // determine whether the row has pKey 
        IF (PTMP == NULL)
        {
            continue;
        }
        PTMP = PTMP + strlen (pKey);     // Wang qing = ==> "= qing" 
        
        PTMP = the strchr (PTMP, ' = ' );     // determines whether there behind pKey = 
        IF (PTMP == NULL)
        {
            continue;
        }
        
        PTMP = PTMP + . 1 ;     // equal sign = number moves the pointer after a

        // get the start value of the removal of space, encountering the first non-space character position given pBegin 
        the while ( 1 )
        {
            if (*pTmp == ' ')
            {
                pTmp++;
            }
            else
            {
                pBegin = pTmp;
                if (*pBegin == '\n')
                {
                    //-2 means the value is empty
                    fclose(fp);
                    right = - 2 ;
                    Return the right;
                }
                break;
            }
        }
        
        // Get value end
         // newline or spaces termination (value value without a line break spaces) 
        the while ( . 1 )
        {
            if ((*pTmp == ' ') || (*pTmp == '\n'))
            {
                break;
            }
            else
            {
                pTmp++;
            }
        }
        pEnd = pTmp;
        
        * pValueLen = End - pBegin;
        memcpy(pValue, pBegin, pEnd-pBegin);
    }
    if (fp != NULL)
    {
        fclose(fp);
    }
    return 0;
}

/*
Write configuration file
Each row write cycle, check whether there is a key configuration items, if present, the modified value corresponding to a value
If not, add "key = value" at the end of the file
*/
int WriteCfgItem(char *pFileName, char *pKey, char *pValue, int ValueLen)
{
    int rv = 0, iTag = 0, length = 0;
    FILE *fp = NULL;
    char lineBuf[MaxLine];
    char *pTmp = NULL, *pBegin = NULL, *pEnd = NULL;
    char filebuf[1024*8] = {0};

    if (pFileName == NULL || pKey == NULL || pValue == NULL)
    {
        rv = -1;
        printf("setCfgItem() error. param error \n");
        return rv;
    }

    fp = fopen(pFileName, "r+");
    if (fp == NULL)
    {
        printf("The file %s is not exist, now create it.\n", pFileName);

        fp = fopen(pFileName, "w+t");
        if (fp == NULL)
        {
            rv = -2;
            printf("create new file %s error. \n",pFileName);
            return rv;
        }
    }
    
    fseek (FP, 0L , SEEK_END);     // the file pointer from the beginning of the file to the end of 
    length = ftell (FP);         // get the file length 
    IF (length> 1024 * . 8 )
    {
        rv = -3;
        printf("the size of file is too big, and no support\n");
        fclose(fp);
        return rv;
    }

    fseek(fp, 0L, SEEK_SET);

    while (!feof(fp))
    {
        memset (lineBuf, 0 , sizeof (lineBuf));
        pTmp = fgets(lineBuf, MaxLine, fp);
        if (pTmp == NULL)
        {
            break;
        }

        / * Check whether the Bank pKey
        Not to copy the filebuf
        Prior modification, copy to the filebuf (alternative)
        After reading the entire file
        */
        pTmp = strstr(lineBuf, pKey);
        if (pTmp == NULL)
        {
            strcat (filebuf, lineBuf);
            continue;
        }
        else
        {
            sprintf (lineBuf, " % S =% S \ n- " , pKey, pValue);
             // the append linebuf filebuf copied into the             
            strcat (filebuf, lineBuf);
            iTag = 1 ;
        }
    }

    // keyword is not present, an additional 
    IF (iTag == 0 )
    {
        fprintf(fp, "%s = %s\n", pKey, pValue);
    }
    the else     // exist if the keyword is re-created file to close the source file and re-written from scratch in the file 
    {
         IF (fp! = NULL)
        {
            fclose(fp);
            fp = NULL;
        }

        fp = fopen(pFileName, "w+t");
        if (fp == NULL)
        {
            rv = -4;
            printf("fopen() err. \n");
            if (fp != NULL)
            {
                fclose(fp);
                return rv;
            }
        }
        fputs(filebuf, fp);
    }

    if (fp != NULL)
    {
        fclose(fp);
    }
    return rv;
}

 

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11920673.html