INI file read-write C + +

Source: Internet
Author: User
Tags sprintf

#include <windows.h>
#include <stdio.h>
int Getprivateprofileint (char *ckey, char *cfield, int ndefault,char *filename)
{
int nret = Ndefault;
Char str[1024], ctmp[200], *pc=null;
FILE *fp=null;
int nlen,len=0;
if (!ckey | |!cfield) return nret;

if (fp = fopen (filename, "r")) = = NULL) return nret;
str[0]= ' + ';
sprintf (CTMP, "[%s]", ckey);
while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
if ((str[len-1]>=0x21) && (str[len-1]<=0x7e)) str[len]=0;
else str[len-1] = 0;

if (str[0] = = '; ' | | str[0] = = ' + ') continue;

if (_strnicmp (str, Ctmp,strlen (CTMP))! = 0) Continue;

while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
if ((str[len-1]>=0x21) && (str[len-1]<=0x7e)) str[len]=0;
else str[len-1] = 0;
if (str[0] = = '; ' | | str[0] = = ' + ') continue;
if (str[0] = = ' [') break;
PC = Strstr (str, "=");
if (PC = = NULL) continue;
if ((PC-STR) < 1) continue;
if (_strnicmp (Cfield, str, pc-str) = = 0)
{
len = strlen (str)-1;
if ((PC-STR) < Len) {
NLEN=SSCANF (pc+1, "0x%x", &nret);
if (nlen==0) nlen=sscanf (pc+1, "0X%x", &nret);
if (nlen==0) nlen=sscanf (pc+1, "%d", &nret);
}
Break
}
}
Break
}
Fclose (FP);
return nret;
}

DWORD getprivateprofilestring (Char *ckey, Char *cfield, char *cdefault,char *cret,dword Dretlen,char *filename)
{
Char str[1024], ctmp[200], *pc=null;
FILE *fp=null;
DWORD len=0;

Cret[0] = 0;
if (Cdefault) strcpy (Cret, Cdefault);
if (!ckey | |!cfield) return 0;

if (fp = fopen (filename, "r")) = = NULL) return 0;
str[0]= ' + ';
sprintf (CTMP, "[%s]", ckey);
while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
if ((str[len-1]>=0x21) && (str[len-1]<=0x7e)) str[len]=0;
else str[len-1] = 0;
if (str[0] = = '; ' | | str[0] = = ' + ') continue;
if (_stricmp (str, CTMP)! = 0) Continue;
while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
if ((str[len-1]>=0x21) && (str[len-1]<=0x7e)) str[len]=0;
else str[len-1] = 0;
if (str[0] = = '; ' | | str[0] = = ' + ') continue;
if (str[0] = = ' [') break;
PC = Strstr (str, "=");
if (PC = = NULL) continue;
if ((PC-STR) < 1) continue;
if (_strnicmp (Cfield, str, pc-str) = = 0)
{
Len = strlen (pc+1);
if (Len < Dretlen) strcpy (CRET, PC + 1);
Break
}
}
Break
}
Fclose (FP);
Return (strlen (CRET)-1);
}

int FindKey (char *ckey, char *filename)
{
int nfind = 0;
Char str[1024], ctmp[100];
FILE *fp=null;
DWORD Nlen;
DWORD Nhlen;
int len=0;
int lastpos = 0;
if (!ckey) return nfind;

if (fp = fopen (filename, "r")) = = NULL) return nfind;
Nlen = GetFileSize (Filename,&nhlen);
if (nlen <= 0) {fclose (FP); return nfind;}

 str[0]= ' + ';
 sprintf (CTMP, "[%s]", ckey);
 while (fgets (str, 1024x768, FP))
 {
  len = strlen (str);
  str[len-1] = 0;
  if (str[0]! = '; ' && str[0]! = ' + ')
  {   
    if (nfind==0)
   {
    if (_stricmp (str, cTmp) = = 0) {
      nfind = 1;
    }
   }
   else
   {
    if (str[0] = = ' [')
     {
     break;
    }
   }
  }
  lastpos = Ftell (FP);
 }
 fclose (FP);
 return Nfind;
}

void *cbuf (unsigned int nSize)
{
void *m_paddr;
int m_nlen = nSize;
M_PADDR = NULL;
if ((M_nlen) > 0)
{
M_PADDR = malloc (nSize);
if (!M_PADDR) {
(M_nlen) = 0;
return NULL;
}
memset (m_paddr, 0, sizeof (nSize));
}
return (M_PADDR);
}

BOOL Writeprivateprofileint (Char *ckey, char *cfield, int nvalue,char *filename)
{

FILE *fp=null;
int Len, Newlen,hinewlen;
Char str[1024], cnewstr[200], ctmp[100], *pc=null;
void *m_paddr;

int nfind = 0;
int nfindnextkey = 0;
Char tmpstr[100];

if (!ckey | |!cfield) return FALSE;
sprintf (Cnewstr, "%s=%d\n", Cfield, Nvalue);
sprintf (CTMP, "[%s]", ckey);
if (! FindKey (ckey, filename))
{
if (fp = fopen (filename, "a +") = = NULL) return FALSE;
fprintf (FP, "\n[%s]\n", Ckey);
fprintf (FP, "%s", CNEWSTR);
Fclose (FP);
return TRUE;
}

if (fp = fopen (filename, "r+") = = NULL) return FALSE;
Newlen = GetFileSize (filename, (unsigned long *) &hinewlen) + strlen (cnewstr) +1000;
M_paddr=cbuf (Newlen);
if (m_paddr==null) return FALSE;

while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
Str[len-1] = 0;
strcat ((char *) m_paddr, str);
strcat (char *) m_paddr, "\ n");
if (_stricmp (str, cTmp) = = 0) break;
}

while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
Str[len-1] = 0;
if (str[0] = = ' + ') continue;
if (str[0] = = ' [' && _stricmp (str, CTMP))
{
Nfindnextkey = 1;
Break
}
PC = Strstr (str, "=");
if (PC && (PC-STR) >=1)
{
memset (tmpstr, 0, 100);
strncpy (Tmpstr, str, PC-STR);
}
if (PC && (pc-str) >= 1 && _stricmp (Cfield, tmpstr) = = 0)
{
Nfind = 1;
strcat ((char *) m_paddr, CNEWSTR);
}
Else
{
strcat ((char *) m_paddr, str);
strcat (char *) m_paddr, "\ n");
}
}
if (Nfind = = 0)
{
strcat ((char *) m_paddr, CNEWSTR);
strcat (char *) m_paddr, "\ n");
}

if (Nfindnextkey)
{
strcat (char *) m_paddr, "\ n");
strcat ((char *) m_paddr, str);
strcat (char *) m_paddr, "\ n");
while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
Str[len-1] = 0;
strcat ((char *) m_paddr, str);
strcat (char *) m_paddr, "\ n");
}
}
Fclose (FP);

fp = fopen (filename, "w+");
Fwrite (M_paddr, strlen (char*) m_paddr), 1, FP);
Fclose (FP);
Free (M_PADDR);
return TRUE;
}

BOOL writeprivateprofilestring (Char *ckey, Char *cfield, char *cvalue,char *filename)
{

 file *fp=null;
 int Len, Newlen,hinewlen;
 char str[1024], cnewstr[200], ctmp[100], *pc=null;
 void *m_paddr;
 int nfind = 0;
 int nfindnextkey = 0;
 char tmpstr[100];
 if (!ckey | |!cfield) return FALSE;
 sprintf (cnewstr, "%s=%s\n", Cfield, Cvalue);
 sprintf (CTMP, "[%s]", ckey);
 if (! FindKey (ckey, filename))
 {
  if ((fp = fopen (filename, "a +") = = NULL) return FALSE;
  fprintf (FP, "\n[%s]\n", Ckey);
  fprintf (FP, "%s", CNEWSTR);
  fclose (FP);
  return TRUE;
 }

if (fp = fopen (filename, "r+") = = NULL) return FALSE;
Newlen = GetFileSize (filename, (unsigned long *) &hinewlen) + strlen (cnewstr) +1000;
M_paddr=cbuf (Newlen);
if (m_paddr==null) return FALSE;
while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
Str[len-1] = 0;
strcat ((char *) m_paddr, str);
strcat (char *) m_paddr, "\ n");
if (_stricmp (str, cTmp) = = 0) break;
}

while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
Str[len-1] = 0;
if (str[0] = = ' + ') continue;
if (str[0] = = ' [' && _stricmp (str, CTMP))
{
Nfindnextkey = 1;
Break
}
PC = Strstr (str, "=");
if (PC && (PC-STR) >=1)
{
memset (tmpstr, 0, 100);
strncpy (Tmpstr, str, PC-STR);
}
if (PC && (pc-str) >= 1 && _stricmp (Cfield, tmpstr) = = 0)
{
Nfind = 1;
strcat ((char *) m_paddr, CNEWSTR);
}
Else
{
strcat ((char *) m_paddr, str);
strcat (char *) m_paddr, "\ n");
}
}
if (Nfind = = 0)
{
strcat ((char *) m_paddr, CNEWSTR);
strcat (char *) m_paddr, "\ n");
}

if (Nfindnextkey)
{
strcat (char *) m_paddr, "\ n");
strcat ((char *) m_paddr, str);
strcat (char *) m_paddr, "\ n");
while (Fgets (str, 1024x768, FP))
{
len = strlen (str);
Str[len-1] = 0;
strcat ((char *) m_paddr, str);
strcat (char *) m_paddr, "\ n");
}
}
Fclose (FP);

fp = fopen (filename, "w+");
Fwrite (M_paddr, strlen (char*) m_paddr), 1, FP);
Fclose (FP);
Free (M_PADDR);
return TRUE;
}

INI file read-write C + +

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.