- 论坛徽章:
- 0
|
LZ说的是读取配置文件的函数吧, 给个例子
BOOL GetPrivateProfileString2(
LPSTR aszSession,
LPSTR aszKey,
LPSTR aszDefault,
char * aszText,
int nSize,
LPSTR aszFileName)
{
FILE * fp;
char szLineBuf[255] = "";
int iInSession = 0;
int iHaveKey = 0;
int i ,j;
int iLen = 0;
int iBegin = 0;
char szSession[40] = "";
char szKey[40] = "";
char szText[255] = "";
strcpy(aszText,aszDefault);
if ((fp = fopen(aszFileName,"r")) == NULL) return FALSE;
while(fgets(szLineBuf,254,fp) != NULL)
{
/*分析输入的字符行*/
iLen = strlen(szLineBuf);
/*取第一个字符*/
i = 0;
for (;i < iLen && szLineBuf[i] == ' ' && szLineBuf[i] < '!' ;i ++ );
if (i == iLen ) continue;
if (szLineBuf[i] == '#') continue;
if (szLineBuf[i] == '[')
{
if (iInSession)
{
fclose(fp);
return FALSE;
}
/*取Session*/
for (j = i + 1;(j < iLen && szLineBuf[j] != ']');j ++);
memcpy(szSession,szLineBuf + i + 1,j - i - 1);
szSession[j - i - 1] = '\0';
if (strcmp(szSession,aszSession) == 0) iInSession = 1;
continue;
}
else if (iInSession)
{
/*取Key*/
for (j = i ;(j < iLen && (szLineBuf[j] != ' ' && szLineBuf[j] != '=' ) && szLineBuf[j] >= '0');j ++);
memcpy(szKey,szLineBuf + i ,j - i );
szKey[j - i ] = '\0';
i = j;
if (strcmp(szKey,aszKey) == 0)
{
for (;i < iLen && ((unsigned char)szLineBuf[i] < '-' || szLineBuf[i] == '=') ;i ++ );
for (j = i ;j < iLen && (szLineBuf[j] != '#') ;j ++);
if (j <=i)
{
strcpy(aszText,"");
}
else
{
memcpy(szText,szLineBuf + i ,j - i - 1);
rtrim(szText,0);
szText[j - i - 1] = '\0';
strcpy(aszText,szText);
}
iHaveKey = 1;
break;
}
}
else
{
continue;
}
}
fclose(fp);
rtrim(aszText,0);
if (iHaveKey) return TRUE;
else return FALSE;
}
BOOL WritePrivateProfileString2(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName)
{
char *token, *pLineToken;
FILE *stream;
char szBuffer[1024];
char szAppSeps[]="[";
char szKeySeps[]="=";
DWORD nLen;
char *pBuf, *pFileBuf;
char *pSectionPos, *pKeyPos;
unsigned long ulFileLen,i,k;
BOOL bSectionFound = FALSE;
BOOL bKeyFound = FALSE;
if((stream = fopen( lpFileName, "r+b" )) == NULL )
{
return FALSE;
}
pBuf = szBuffer;
fseek(stream, 0, SEEK_END);
ulFileLen = ftell(stream);
pFileBuf = (char*)malloc(ulFileLen);
fseek(stream, 0, SEEK_SET);
fread(pFileBuf, ulFileLen, 1, stream);
fclose(stream);
pLineToken = pFileBuf;
while(pLineToken)
{
if(!bSectionFound)
{
pSectionPos = pLineToken;
}
else
{
pKeyPos = pLineToken;
}
pLineToken = strtok(pLineToken, "\n");
if(!pLineToken) break;
strcpy(pBuf, pLineToken);
pLineToken+=strlen(pLineToken)+1;
LTruncate(pBuf, " \t");
token = strstr(szBuffer, "//");
if(token) token[0] = 0;
if(pBuf = strchr(szBuffer, '\\'))
{
if(pBuf[1]!='x' && pBuf[1]!='X')
continue;
}
pBuf = szBuffer;
if(strlen(szBuffer)==0) continue;
if(!bSectionFound)
{
token = strtok(szBuffer, szAppSeps);
if(token == szBuffer)
continue;
LTruncate(token, " \t");
RTruncate(token, " ]\t\r\n");
if(strcompi(token, lpAppName))
continue;
else
bSectionFound = TRUE;
}
else
{
nLen = strlen(szBuffer);
token = strtok(szBuffer, szKeySeps);
if(strlen(token) == nLen)
{ /* 判断是否离开指定的Section */
token = strtok(szBuffer, szAppSeps);
if(token != szBuffer)
break;
else
continue;
}
LTruncate(szBuffer, " \t");
RTruncate(szBuffer, " \t");
if(strcompi(szBuffer, lpKeyName))
{
continue;
}
else
{ /* 找到 */
bKeyFound = TRUE;
break;
}
}
}
if((stream = fopen( lpFileName, "wb" )) == NULL )
{
free(pFileBuf);
return FALSE;
}
if(!bSectionFound && !bKeyFound)
{
for(i=0;i<ulFileLen;i++)
{
if(pFileBuf[i]==0) pFileBuf[i]='\n';
}
fwrite(pFileBuf, i, 1, stream);
fseek(stream, 0, SEEK_END);
sprintf(szBuffer, "[%s]\n", lpAppName);
fwrite(szBuffer, strlen(szBuffer), 1, stream);
sprintf(szBuffer, "%s=%s\n", lpKeyName, lpString);
fwrite(szBuffer, strlen(szBuffer), 1, stream);
}
else if(bSectionFound && bKeyFound)
{
for(i=0;pFileBuf+i<pKeyPos;i++)
{
if(pFileBuf[i]==0) pFileBuf[i]='\n';
}
fwrite(pFileBuf, i, 1, stream);
sprintf(szBuffer, "%s=%s\n", lpKeyName, lpString);
fwrite(szBuffer, strlen(szBuffer), 1, stream);
k=0;
for(i+=(strlen(pKeyPos)+1);i<ulFileLen;i++)
{
if(pFileBuf[i]==0) pFileBuf[i]='\n';
k++;
}
fwrite(pKeyPos+strlen(pKeyPos)+1, k, 1, stream);
}
else if(bSectionFound && !bKeyFound)
{
for(i=0;pFileBuf+i<pKeyPos;i++)
{
if(pFileBuf[i]==0) pFileBuf[i]='\n';
}
fwrite(pFileBuf, i, 1, stream);
sprintf(szBuffer, "%s=%s\n", lpKeyName, lpString);
fwrite(szBuffer, strlen(szBuffer), 1, stream);
k=0;
for(;i<ulFileLen;i++,k++)
{
if(pFileBuf[i]==0) pFileBuf[i]='\n';
}
fwrite(pKeyPos, k, 1, stream);
}
fclose(stream);
free(pFileBuf);
return TRUE;
} |
|