首页 > 代码库 > INI文件读写 c++

INI文件读写 c++

#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]=‘\0‘;
 sprintf(cTmp, "[%s]", cKey);
 while( fgets(str, 1024, 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] == ‘\0‘) continue;

  if( _strnicmp(str, cTmp,strlen(cTmp)) != 0) continue;
  
  while( fgets(str, 1024, 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] == ‘\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]=‘\0‘;
 sprintf(cTmp, "[%s]", cKey);
 while( fgets(str, 1024, 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] == ‘\0‘) continue;
  if( _stricmp(str, cTmp) != 0) continue;
  while( fgets(str, 1024, 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] == ‘\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]=‘\0‘;
 sprintf(cTmp, "[%s]", cKey);
 while( fgets(str, 1024, fp) )
 {
  len = strlen(str);
  str[len-1] = 0;
  if( str[0] != ‘;‘ && str[0] != ‘\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, 1024, 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, 1024, fp) )
 {  
  len = strlen(str);
  str[len-1] = 0;
  if( str[0] == ‘\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, 1024, 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, 1024, 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, 1024, fp) )
 {  
  len = strlen(str);
  str[len-1] = 0;
  if( str[0] == ‘\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, 1024, 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文件读写 c++