Win32 文件(3)

8.15 如何创建临时文件

GetTempPath和GetTempFileName

void CDemoDlg::OnCreateTmpFile() 
{
    TCHAR szPathName[MAX_PATH];
    TCHAR szFileName[MAX_PATH];
    //获得临时文件目录
    if (!::GetTempPath(MAX_PATH, szPathName))
    {
        return;
    }
    //创建临时文件名并在目录中创建文件
    if (!::GetTempFileName(szPathName, _T("~ex"), 0, szFileName))
    {
        return;
    }
    CString strText = _T("");
    strText.Format(_T("临时文件:\n%s"), szFileName);
    AfxMessageBox(strText);
}

8.16 如何创建目录

CreateDirectory方法

void CDemoDlg::OnCreateDir() 
{
    CString strDirectory = _T("Demo");
    //查找文件
    CFileFind finder;
    if (finder.FindFile(strDirectory))
    {
        finder.FindNextFile();
        if (finder.IsDirectory())
        {
            AfxMessageBox(_T("目录已经存在。"));
        }
        else
        {
            AfxMessageBox(_T("目录名与现有的文件名重名。"));
        }
    }
    else
    {
        //创建目录
        if (::CreateDirectory(strDirectory, NULL))
        {
            AfxMessageBox(_T("创建目录成功。"));
        }
        else
        {
            AfxMessageBox(_T("创建目录失败。"));
        }
    }
}

8.17 如何删除目录

RemoveDirectory方法

BOOL CDemoDlg::DeleteTree(CString strDirectory)
{
    CString strWildcard = strDirectory;
    strWildcard += _T("\\*.*");
    CFileFind finder;
    BOOL bFind = FALSE;
    //查找文件
    bFind = finder.FindFile(strWildcard);
    while (bFind)
    {
        //查找下一个文件
        bFind = finder.FindNextFile();
        if (finder.IsDots())
        {
            continue;
        }
        //找到文件的路径
        CString strPathName = finder.GetFilePath();
        //获得找到文件的名称
        if (finder.IsDirectory())
        {
            //递归删除目录
            if (!DeleteTree(strPathName))
            {
                return FALSE;
            }
        }
        else
        {
            if (!::DeleteFile(strPathName))
            {
                return FALSE;
            }        
        }
    }
    //结束查找
    finder.Close();
    //删除空目录
    if (!::RemoveDirectory(strDirectory))
    {
        return FALSE;
    }
    return TRUE;
}
void CDemoDlg::OnDeleteDir() 
{
    CString strDirectory = _T("C:\\Demo");
    if (DeleteTree(strDirectory))
    {
        AfxMessageBox(_T("删除目录成功。"));
    }
    else
    {
        AfxMessageBox(_T("删除目录失败。"));
    }
}

8.18 如何逐行读取文本文件

CStdioFile类的使用

void CDemoDlg::OnReadFile() 
{
    CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
    pListBox->ResetContent();
    //创建文件对话框
    CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | 
        OFN_OVERWRITEPROMPT, _T("文本文件(*.*)|*.*||"));
    if (dlg.DoModal() == IDOK)
    {
        //获得文件路径
        CString strPathName = dlg.GetPathName();
        CStdioFile file;
        //打开文件
        if (!file.Open(strPathName, CFile::modeRead))
        {
            ::AfxMessageBox(_T("文件打开失败。"));
            return;
        }
        //读文件
        CString strText = _T("");
        while (file.ReadString(strText))
        {
            pListBox->AddString(strText);
        }
        //关闭文件
        file.Close();
    }
}

8.19 如何在INI文件中读写数据

INI文件犹如xml功能,config文件
GetPrivateProfileString和WritePrivateProfileString两个读写方法

void CDemoDlg::OnTest1() 
{
    CString strSectionName = _T("");
    GetDlgItemText(IDC_TEXT1, strSectionName);
    CString strKeyName = _T("");
    GetDlgItemText(IDC_TEXT2, strKeyName);
    CString strKeyValue = _T("");
    //在INI文件中读字符串数据
    ::GetPrivateProfileString(strSectionName, strKeyName, _T(""), 
        strKeyValue.GetBuffer(1024), 1024, m_strFileName);
    strKeyValue.ReleaseBuffer();
    SetDlgItemText(IDC_TEXT3, strKeyValue);
}
void CDemoDlg::OnTest2() 
{
    CString strSectionName = _T("");
    GetDlgItemText(IDC_TEXT1, strSectionName);
    CString strKeyName = _T("");
    GetDlgItemText(IDC_TEXT2, strKeyName);
    CString strKeyValue = _T("");
    GetDlgItemText(IDC_TEXT3, strKeyValue);
    //在INI文件中读字符串数据
    ::WritePrivateProfileString(strSectionName, strKeyName, strKeyValue, m_strFileName);
}

8.20 如何获得INI文件的全部段名

GetPrivateProfileSectionNames方法

void CDemoDlg::OnTest() 
{
    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    pList->DeleteAllItems();
    
    TCHAR szBuffer[1024] = {0};
    TCHAR szSectionName[128] = {0};
    //获得INI文件的全部段名
    int nBufferSize = GetPrivateProfileSectionNames(szBuffer, 1024, m_strFileName);
    int nItem = 0;
    for (int n = 0, i = 0; n < nBufferSize; n++)
    {
        if (szBuffer[n] == 0)
        {
            szSectionName[i] = 0;
            pList->InsertItem(n, szSectionName);
            i = 0;
            nItem++;
        }    
        else
        {
            szSectionName[i] = szBuffer[n];
            i++;
        }
    }
}

8.21 如何获得INI文件指定段的全部键名和键值

GetPrivateProfileSection方法,值得注意的是取出来的键值是以一个字符串的形式而存在,以0隔开

void CDemoDlg::OnTest() 
{
    CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    pList->DeleteAllItems();
    TCHAR szKey[1024] = {0};
    CString strKey = _T("");
    CString strKeyName = _T("");
    CString strKeyValue = _T("");
    TCHAR szBuffer[65536] = {0};
    CString strSectionName = _T("");
    GetDlgItemText(IDC_TEXT, strSectionName);
    //获得INI文件指定段的全部键名和键值
    int nBufferSize = GetPrivateProfileSection(strSectionName, szBuffer, 
        65536, m_strFileName);
    int nItem = 0;
    for (int n = 0, i = 0; n < nBufferSize; n++)
    {
         if (szBuffer[n] == 0)
         {
             szKey[i] = 0;
            strKey = szKey;
            strKeyName = strKey.Left(strKey.Find('='));
            strKeyValue = strKey.Mid(strKey.Find('=') + 1);
            pList->InsertItem(nItem, strKeyName);
            pList->SetItemText(nItem, 1, strKeyValue);
            i = 0;
            nItem++;
         }
        else
        {
            szKey[i] = szBuffer[n];
            i++;
        }
    }
}

原文地址:https://www.cnblogs.com/Clingingboy/p/1998144.html