evc读写INI 文件 参考代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DWORD GetIniValue(const char * lpAppName,const char * lpKeyName,const char * lpDefault,char * lpReturnedString,DWORD nSize,const char * lpFileName)
{
FILE *fp;
BOOL bSection,bFind;
int nItemLen,nPos,nLine;
char szItem[256],szSection[100];//szItem[2048]
char szTempString[256];
char *pStr,*pLine;
bSection=FALSE; bFind=FALSE;
if(strlen(lpAppName)<=0||strlen(lpKeyName)<=0)
{ strcpy(lpReturnedString,lpDefault); return (DWORD)(strlen(lpDefault)); }
if((fp=fopen(lpFileName,"rb+"))==NULL)
{ strcpy(lpReturnedString,lpDefault); return (DWORD)(strlen(lpDefault)); }
while(!feof(fp))
{
memset(szTempString,0,sizeof(szTempString));
fgets(szTempString,256,fp);
pLine=strstr(szTempString,"\r\n");
if(pLine!=NULL)
{
nLine=pLine-szTempString;
memset(szItem,0,sizeof(szItem)); strncpy(szItem,szTempString,nLine);
nItemLen=strlen(szItem);
if(szItem[0]=='['&&szItem[nItemLen-1]==']')
{
if(!bSection)
{
memset(szSection,0,sizeof(szSection));
strncpy(szSection,szItem+1,nItemLen-2);
if(strcmp(szSection,lpAppName)==0)bSection=TRUE;
}
else
break;
}
else
{
if(!bSection)continue;
else
{
pStr=strstr(szItem,"=");
if(pStr!=NULL)
{
nPos=pStr-szItem;
memset(szSection,0,sizeof(szSection));
strncpy(szSection,szItem,nPos);
if(strcmp(szSection,lpKeyName)==0)
{
if((nItemLen-nPos-1) >= (int)nSize)
{
strncpy(lpReturnedString,szItem+nPos+1,nSize);
fclose(fp);
return nSize;
}
else
{
strncpy(lpReturnedString,szItem+nPos+1,nItemLen-nPos-1);
fclose(fp);
return (DWORD)(nItemLen-nPos-1);
}
}
}
}
}
}
}
fclose(fp);
strcpy(lpReturnedString,lpDefault);
return (DWORD)(strlen(lpDefault));
}
//lpKeyName:要查哪个字段
//参数:lpFileName:文件,
//*lpValue:要写的字串,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
BOOL SetIniValue(const char * lpAppName,const char *lpKeyName, const char *lpValue,char *lpFileName)
{
FILE *fp,*fpNew;
BOOL bSection,bFind;
int nItemLen,nPos,nLine;
TCHAR pPath[100];
TCHAR szBuf[100];
TCHAR pFile[100],pTempFile[100];
char szTempFile[100];
char szTempString[256];
char szItem[256],szSection[200];
char szTemp[256];
char *pStr,*pLine;
bSection=FALSE;
bFind=FALSE;
if(strlen(lpAppName)<=0||strlen(lpKeyName)<=0)
{
return FALSE;
}
if((fp=fopen(lpFileName,"rb+"))==NULL)
{
return FALSE;
}
memset(szBuf, 0, sizeof(szBuf));
memset(pPath, 0, sizeof(pPath));
GetModuleFileName(NULL, szBuf, sizeof(szBuf)/sizeof(TCHAR));
wcsncpy(pPath, szBuf, wcslen(szBuf) - wcslen(wcsrchr(szBuf, (TCHAR)'\\')) + 1);
memset(szTempFile, 0, sizeof(szTempFile));
wcstombs(szTempFile, pPath, 100);
sprintf(szTempFile,"%s%s",szTempFile,"temp.ini");
fpNew=fopen(szTempFile,"wb+");
while(!feof(fp))
{
memset(szTempString,0,sizeof(szTempString));
fgets(szTempString,256,fp);/*fGetLine(szItem,256,fp);*/
pLine=strstr(szTempString,"\r\n");
if(pLine!=NULL)
{
nLine=pLine-szTempString;
memset(szItem,0,sizeof(szItem)); strncpy(szItem,szTempString,nLine);
// fwrite(szItem,1,strlen(szItem),fpTest);
// fwrite("\r\n",1,2,fpTest);
nItemLen=strlen(szItem);
if(szItem[0]=='['&&szItem[nItemLen-1]==']')
{
fwrite(szItem,1,strlen(szItem),fpNew);
fwrite("\r\n",1,2,fpNew);
if(!bSection)
{
memset(szSection,0,100); strncpy(szSection,szItem+1,nItemLen-2);
if(strcmp(szSection,lpAppName)==0)bSection=TRUE;
}
else
{
// bSection=FALSE;
}
//break;
}
else
{
if(!bSection)
{
fwrite(szItem,1,strlen(szItem),fpNew);
fwrite("\r\n",1,2,fpNew);
continue;
}
else
{
pStr=strstr(szItem,"=");
if(pStr!=NULL)
{
nPos=pStr-szItem;
memset(szSection,0,100); strncpy(szSection,szItem,nPos);
if(strncmp(szSection,lpKeyName,strlen(lpKeyName))==0)
{
memset(szTemp,0,strlen(szTemp));
strcpy(szTemp,lpKeyName);
strcat(szTemp,"=");
strcat(szTemp,lpValue);//strncat(szTemp,lpValue,strlen(lpValue));
fwrite(szTemp,1,strlen(szTemp),fpNew);
fwrite("\r\n",1,2,fpNew);
//if((nItemLen-nPos-1) >= (int)nSize)
//{ strncpy(lpReturnedString,szItem+nPos+1,nSize); fclose(fp); return nSize; }
//else
//{ strncpy(lpReturnedString,szItem+nPos+1,nItemLen-nPos-1); fclose(fp); return (DWORD)(nItemLen-nPos-1); }
}
else
{
fwrite(szItem,1,strlen(szItem),fpNew);
fwrite("\r\n",1,2,fpNew);
}
}
}
}
}
}
fclose(fp);
fclose(fpNew);
// fclose(fpTest);
memset(pFile, 0, sizeof(pFile));
mbstowcs(pFile, lpFileName, 100);
memset(pTempFile, 0, sizeof(pTempFile));
mbstowcs(pTempFile, szTempFile, 100);
DeleteFile(pFile);
MoveFile(pTempFile,pFile);
return TRUE;
}