INI文件和TXT的使用总结

最近一直在一个上位机的软件,看情形还要做一段时间(需求不断在变和增加大哭),正好今天有点时间,就把INI文件的使用

坐下总结,很简单的东东,老鸟直接飞过就可以。

一、INI文件对编码是有格式要求

这个坑把我害的不浅,我是直接把txt改为ini文件了,然后去操作,发现如果是英文的可以,如果是中文的总是出错。

无奈,最后从系统里找了个INI文件,直接把内容替换掉,可以了。


二、文件创建(要包括完整路径和文件名称)


bool StringOpr::userCreatFile(HWND hWnd,CString   strFilePath)
{
    if(!strFilePath.GetLength()){
        return false;
    }

    CFileFind   myTxtFileFind;
    CFile       myFile;

    if(myTxtFileFind.FindFile(strFilePath)){
        int iUserSelect = 0;
        iUserSelect = MessageBox(hWnd,"文件存在,是否覆盖?","系统提示",MB_YESNO|MB_ICONQUESTION);
        if(IDYES == iUserSelect){
            myFile.Open(strFilePath,CFile::modeCreate|CFile::modeReadWrite);
            myFile.Close();
        }else{
            myFile.Open(strFilePath,CFile::modeReadWrite);
            myFile.Close();
        }
    }else{
        myFile.Open(strFilePath,CFile::modeCreate|CFile::modeReadWrite);
        myFile.Close();
    }
    return true;
}


三、查找指定文件夹下的所有指定类型文件

我是添加到了CComboBox控件里面,当然也可以改为其他的。

bool StringOpr::userFineFileList(CComboBox* pAddCComboBox,CString   strDocumentPath,CString  strFileType)
{
    if((NULL == pAddCComboBox) || (!strDocumentPath.GetLength()) || (!strFileType.GetLength())){
        return  false;
    }

    CFileFind   myCFileFindTemp;
    CString     myCStringTemp;
    BOOL        bIsFind = true;
    
    pAddCComboBox->ResetContent();

    myCStringTemp = strDocumentPath + "\\*." + strFileType;

    if(!myCFileFindTemp.FindFile(myCStringTemp)){//表示有没有找到指定文件
        pAddCComboBox->AddString("Customize");
        return false;
    }

    while(bIsFind){
        bIsFind = myCFileFindTemp.FindNextFile();//表示当前文件的下面还有没有文件
        if(bIsFind){
            myCStringTemp = myCFileFindTemp.GetFileTitle();
            pAddCComboBox->AddString(myCStringTemp);
        }else{                                  //最后一个文件的下面没有文件了,所以返回false
            myCStringTemp = myCFileFindTemp.GetFileTitle();
            pAddCComboBox->AddString(myCStringTemp);
        }
    }
    pAddCComboBox->AddString("Customize");
    return true;
}


三、创建文件夹


bool StringOpr::userCreatDocument(CString   strDocumentPath)
{
    if(!strDocumentPath.GetLength()){
        return false;
    }

    if(!PathFileExists(strDocumentPath))//文件夹不存在则创建
    {
        return CreateDirectory(strDocumentPath,NULL);
    }//strPath_txt文件夹路径

    return true;
}


四、打开选择文件夹,返回路径


bool StringOpr::userOpenDocument(CString   *pStrDocumentPath)
{
    if(NULL == pStrDocumentPath){
        return false;
    }

    // 获取特定文件夹的LPITEMIDLIST,可以将之理解为HANDLE  
    // 所谓的特定文件夹,你可以用CSIDL_XXX来检索之。  
    LPITEMIDLIST rootLoation;  
    SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &rootLoation);  
    if (rootLoation == NULL) {
        // unkown error  
        return false;  
    }  
    // 配置对话框  
    BROWSEINFO bi;  
    ZeroMemory(&bi, sizeof(bi));  
    bi.pidlRoot = rootLoation; // 文件夹对话框之根目录,不指定的话则为我的电脑  
    bi.lpszTitle = _T("选择文件夹?"); // 可以不指定  
    // bi.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS;  

    // 打开对话框, 有点像DoModal  
    LPITEMIDLIST targetLocation = SHBrowseForFolder(&bi);  
    if (targetLocation != NULL) {  
        TCHAR targetPath[MAX_PATH];  
        SHGetPathFromIDList(targetLocation, targetPath);  
        //MessageBox( targetPath );  
        //GetDlgItem(IDC_RICHEDIT_DATA_SHOW)->SetWindowText(targetPath);//将路径显示
        (*pStrDocumentPath) = targetPath;
        return true;
    }
    return false;
}


五、找到INI文件下所有匹配的节名

与strAppNameAsteriskWildcard匹配,并添加到CComboBox控件

bool StringOpr::userFineIniFileTitleList(CComboBox* pAddCComboBox,CString   strIniFilePath,CString  strAppNameAsteriskWildcard)
{
    if((NULL == pAddCComboBox) || (!strIniFilePath.GetLength())){
        return  false;
    }

    pAddCComboBox->ResetContent();

    char*       cChar = new char[5000];
    DWORD       i=0,j=0;
    CString     strTemp;
    i   =   GetPrivateProfileSectionNames(cChar,sizeof(cChar)-1000,strIniFilePath);//最多1000节,4000字节长度

    if(!i){
        delete cChar;
        return  false;
    }
    strTemp.Empty();
    for(j=0;j<i;j++){
        if(0 == cChar[j]){
            if('*' == strAppNameAsteriskWildcard || -1 != strTemp.Find(strAppNameAsteriskWildcard)){
                pAddCComboBox->AddString(strTemp);
            }
            strTemp.Empty();
            continue;
        }
        strTemp+=cChar[j];
    }


    delete cChar;
    return true;
}


六、找到指定节名下的指定健名的键值,根据它确定最大数量


bool StringOpr::userFineIniFileKeyList(CComboBox* pAddCComboBox,CString   strIniFilePath,CString strAppName,CString strKeyName)
{
    if((NULL == pAddCComboBox) || (!strIniFilePath.GetLength()) || (!strAppName.GetLength()) || (!strKeyName.GetLength())){
        return  false;
    }

    pAddCComboBox->ResetContent();

    char        cChar[10];
    uint16_t    i=0,j=0;
    CString     strTemp;
    i   =   (uint16_t)GetPrivateProfileString(strAppName,strKeyName,0,cChar,sizeof(cChar)-1,strIniFilePath);

    if(!i){
        return false;
    }
    strTemp = cChar;
    strTemp.Remove(' ');
    i = _ttoi(strTemp);
    for(j=0;j<i;j++){
        strTemp.Format("%d",(j+1));
        pAddCComboBox->AddString(strTemp);
    }

    return true;
}


七、找到指定节名下的所有键值


bool StringOpr::userFineIniFileKeyList(CComboBox* pAddCComboBox,CString   strIniFilePath,CString strAppName)
{
    if((NULL == pAddCComboBox) || (!strIniFilePath.GetLength())){
        return  false;
    }

    char*       cChar = new char[5000];
    DWORD       i=0,j=0;
    CString     strTemp;
    i   =   GetPrivateProfileSection(strAppName,cChar,sizeof(cChar)-500,strIniFilePath);//最多500节,4500字节长度

    if(!i){
        delete cChar;
        return  false;
    }

    strTemp.Empty();
    for(j=0;j<i;j++){
        if(0 == cChar[j]){
            if(-1 != strTemp.Find('=')){
                strTemp.TrimLeft();
                StringOpr::userCStringRemoveLiftSelf(&strTemp,'=');
                pAddCComboBox->AddString(strTemp);
            }
            strTemp.Empty();
            continue;
        }
        strTemp+=cChar[j];
    }

    delete cChar;
    return true;
}


八、找指定节名下的所有健名和键值

以strReplace作为分隔符

bool StringOpr::userFineIniFileKeyList(CString* pMyCStringTemp,CString strReplace,CString   strIniFilePath,CString strAppName)
{
    if((NULL == pMyCStringTemp) || (!strIniFilePath.GetLength())){
        return  false;
    }

    char*       cChar = new char[5000];
    DWORD       i=0,j=0;
    //CString     strTemp;
    i   =   GetPrivateProfileSection(strAppName,cChar,sizeof(cChar)-500,strIniFilePath);//最多500节,4500字节长度

    if(!i){
        delete cChar;
        return  false;
    }

    pMyCStringTemp->Empty();
    for(j=0;j<i;j++){
        if(0 == cChar[j]){
            (*pMyCStringTemp)+=strReplace;
            continue;
        }
        (*pMyCStringTemp)+=cChar[j];
    }

    delete cChar;
    return true;
}



































  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值