C simple configuration file operations

Source: Internet
Author: User

 

# Include "stdafx. H"
# Include <stdio. h>
# Include <string. h>
# Include <ctype. h>

# Define Upload File "your file. conf"

/*************************************** ************************
* Function name: getinterval Val
* Parameter: cfilename indicates the value of the key value to be queried for the configuration file name key.
* Return value: 1 is returned for success, and the value is saved to value; 0 is returned for failure.
* Function: Read the configuration from the basic configuration file.
**************************************** ************************/
Int get0000val (char * cfilename, char * Key, char * value)
{
File * fopen;
Char cbuf [512] = {0 };
Char tmpbuf [512];
Char * P, * q;
Int nret = 0;

Fopen = fopen (cfilename, "R ");
If (fopen! = NULL)
{
While (! Feof (fopen ))
{
Fgets (cbuf, sizeof (cbuf), fopen );
If (cbuf [0] = '#')
Continue;
Strcpy (tmpbuf, cbuf );
Q = P = strchr (tmpbuf, '= ');
If (P> 0)
{
While (* P & * (PM) = '') p --;
* P = 0;

If (strcmp (tmpbuf, key) = 0)
{
Nret = 1;
Q ++;
While (* Q & * q = '') q ++;
If (Q [strlen (q)-1] = '\ n ')
Q [strlen (q)-1] = 0;
Strcpy (value, q );
}

}
}

} Else
{
Nret = 0;
}
Fclose (fopen );
Return nret;
}

Int main (INT argc, char * argv [])
{
Char Buf [256] = {0 };
Char key [] [256] = {"ip", "Port", "servername", "homedir", "user", "password "};
Int I;

Printf ("\ n \ tpolicfileparse example. \ n ");

For (I = 0; I <6; I ++)
If (getinterval Val (partition file, key [I], Buf ))
Printf ("% s: % s \ n", key [I], Buf );
Else
Printf ("failed to get % s \ n", key [I]);

Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.