C Code reads the INI File

Source: Internet
Author: User

INI file format:

; Comment
[Node name]
Key = Value
...

// Read the value of a specific item in the INI File
Char * getinitkey (char * pfilename, char * playback ction, char * pkey)
{
Static char retstr [1024];
Int nflag = 0;
Char tmpline [1024];
File * pfile;

Pfile = fopen (pfilename, "R ");
If (pfile = NULL)
{
Return NULL;
}

While (1)
{
// Whether the end of the file is
If (fgets (tmpline, 1024, pfile) = NULL)
{
Break;
}
// Ignore the comment line
If (tmpline [0] = ';')
{
Continue;
}

// Whether the node name is found
If (nflag = 0)
{
// Whether the column is a node name
If (tmpline [0] = '[')
{
Int ntitlelen = strlen (Limit ction );
// Check whether the node name length is the same as the name of the queried node, and whether the node name is the same
If (tmpline [ntitlelen + 1] = ']' & strncmp (ction, tmpline + 1, ntitlelen) = 0)
{
Nflag = 1;
}
}
}
Else
{
// Whether it is a key-value column
// Const char * ptemp = strchr (tmpline, '= ');
// If (ptemp! = NULL)
Int nkeylen = strlen (pkey );
If (tmpline [nkeylen] = ')
{
// Ptemp-tmpline = nkeylen &&
If (strncmp (pkey, tmpline, nkeylen) = 0)
{
Strcpy (retstr, tmpline + 1 + nkeylen );
// Remove the last line break
Retstr [strlen (retstr)-1] = 0;
Fclose (pfile );

Return retstr;
}
}
// New node name, jump out
Else if (tmpline [0] = '[')
{
Break;
}
}
}

Fclose (pfile );
Return NULL;
}

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.