zoukankan      html  css  js  c++  java
  • 遍历INI文件和删除指定域内容

    主要还是使用的INI文件操作的API,只是把参数修改下。

    BOOL WINAPI WritePrivateProfileString(
      __in          LPCTSTR lpAppName,
      __in          LPCTSTR lpKeyName,
      __in          LPCTSTR lpString,
      __in          LPCTSTR lpFileName
    );
    
    


    MSDN里这样说的:

    lpKeyName

    The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is NULL, the entire section, including all entries within the section, is deleted.

    就是说如果 lpKeyName为NULL的话,那么就删除 lpAppName 这个域(section)的所有内容。

    这个是关键,我们就靠它来删除指定域(section)的内容。

    lpString

    A null-terminated string to be written to the file. If this parameter is NULL, the key pointed to by the lpKeyName parameter is deleted.

    这个 lpString如果为NULL的话,那么就删除 lpKeyName这内容。

    这个也是关键,我们就靠它来删除指定键(key)的内容。


    下面是具体代码实现:

    	CString filePath = m_ExeFilePath + g_iniFileName;
    	TCHAR strFileName[512] = {0};
    	TCHAR strAppNameTemp[MAX_APPNAME] = {0}; //所有AppName的返回值
        //所有AppName的总长度  
        DWORD dwAppNameSize = GetPrivateProfileString(NULL,NULL,NULL,strAppNameTemp,MAX_APPNAME,filePath);
    	if(dwAppNameSize > 0)
    	{
    		TCHAR* pAppName = new TCHAR[dwAppNameSize];
    		::memset(pAppName,0,dwAppNameSize*sizeof(TCHAR));
    		int nAppNameLen=0;  //每个AppName的长度  
            for(DWORD i = 0;i<dwAppNameSize;i++)  
            {  
                pAppName[nAppNameLen++]=strAppNameTemp[i];  
                if(strAppNameTemp[i]==_T(''))  
                {  
    				::GetPrivateProfileStringW(pAppName,_T("Path"),_T(""),strFileName,512,filePath);   // 资源的路径
    				if(!IsFileExist(strFileName))
    				{
    					// 资源文件不存在,则删除当前的记录
    					::WritePrivateProfileStringW(pAppName,NULL,NULL,filePath);
    				}
    				::memset(pAppName,0,dwAppNameSize*sizeof(TCHAR));
                    nAppNameLen=0;
    			}
    
    		}
    		delete [] pAppName;
    	}


    如果对代码有问题可以留言。


  • 相关阅读:
    [01] 异常的概念和处理
    [03] 节点流和处理流
    [02] 输入/输出流 和 字节/字符流
    [01] File类
    Apache的https协议配置
    Apache的虚拟主机配置
    Apahce的虚拟用户认证及server-status页
    Apache配置日志功能
    Apache脚本路径别名(CGI接口)
    Apache配置站点根目录、用户目录及页面访问属性
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3174612.html
Copyright © 2011-2022 走看看