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;
    	}


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


  • 相关阅读:
    校园网络(tarjan)
    消息扩散(强连通分量)
    上帝造题的七分钟(树桩数组乱搞)
    数颜色 / 维护队列(带修莫队)
    Luogu5155 [USACO18DEC]Balance Beam
    分数规划小结
    Luogu3177 [HAOI2015]树上染色
    Luogu4402 机械排序
    Luogu3201 [HNOI2009]梦幻布丁
    Luogu3380 二逼平衡树
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3174612.html
Copyright © 2011-2022 走看看