VC++中文件操作(二)--- .ini文件、CFile64
各種關於文件的操作在程序設計中是十分常見,如果能對其各種操作都了如指掌,就可以根據實際情況找到最佳的解決方案,從而在較短的時間內編寫出高效的代碼,因而熟練的掌握文件操作是十分重要的。本文將對Visual C++中有關文件操作進行全面的介紹,並對在文件操作中經常遇到的一些疑難問題進行詳細的分析。
VC++中文件操作(二)
***************************************************************************
××××××××××第一、VC有關文件操作,參考資料2
***************************************************************************
在我們寫的程序當中,總有一些配置信息需要保存下來,以便完成程序的功能,最簡單的辦法就是將這些信息寫入INI文件中,程序初始化時再讀入.具體應用如下:
一.將信息寫入.INI文件中.
1.所用的WINAPI函數原型為:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
其中各參數的意義:
LPCTSTR lpAppName 是INI文件中的一個字段名.
LPCTSTR lpKeyName 是lpAppName下的一個鍵名,通俗講就是變量名.
LPCTSTR lpString 是鍵值,也就是變量的值,不過必須為LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
2.具體使用方法:設現有一名學生,需把他的姓名和年齡寫入 c:\stud\student.ini 文件中.
CString strName,strTemp;
int nAge;
strName="張三";
::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");
此時c:\stud\student.ini文件中的內容如下:
[StudentInfo]
Name=張三
3.要將學生的年齡保存下來,只需將整型的值變為字符型即可:
strTemp.Format("%d",nAge);
::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\\stud\\student.ini");
二.將信息從INI文件中讀入程序中的變量.
1.所用的WINAPI函數原型為:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
其中各參數的意義:
前二個參數與 WritePrivateProfileString中的意義一樣.
lpDefault : 如果INI文件中沒有前兩個參數指定的字段名或鍵名,則將此值賦給變量.
lpReturnedString : 接收INI文件中的值的CString對象,即目的緩存器.
nSize : 目的緩存器的大小.
lpFileName : 是完整的INI文件名.
2.具體使用方法:現要將上一步中寫入的學生的信息讀入程序中.
CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","默認姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");
執行後 strStudName 的值為:"張三",若前兩個參數有誤,其值為:"默認姓名".
3.讀入整型值要用另一個WINAPI函數:
UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);
這裏的參數意義與上相同.使用方法如下:
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");
三.循環寫入多個值,設現有一程序,要將最近使用的幾個文件名保存下來,具體程序如下:
1.寫入:
CString strTemp,strTempA;
int i;
int nCount=6;
file://共有6個文件名需要保存
for(i=0;i {strTemp.Format("%d",i);
strTempA=文件名;
file://文件名可以從數組,列表框等處取得.
::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
"c:\\usefile\\usefile.ini");
}
strTemp.Format("%d",nCount);
::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");
file://將文件總數寫入,以便讀出.
2.讀出:
nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");
for(i=0;i {strTemp.Format("%d",i);
strTemp="FileName"+strTemp;
::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");
file://使用strTempA中的內容.
}
補充四點:
1.INI文件的路徑必須完整,文件名前面的各級目錄必須存在,否則寫入不成功,該函數返回 FALSE 值.
2.文件名的路徑中必須為 \\ ,因為在VC++中, \\ 才表示一個 \ .
3.也可將INI文件放在程序所在目錄,此時 lpFileName 參數為: ".\\student.ini".
1 //---------------------------------------------------------------------------------- 2 /* 3 類名:CIni 4 版本:v2.0 5 最後更新: 6 v2.0 7 夢小孩於2004年2月14日情人節 8 加入高級操作的功能 9 v1.0 10 夢小孩於2003年某日 11 一般操作完成 12 13 類描述: 14 本類可以於.ini文件進行操作 15 */ 16 17 文件 1: 18 19 #pragma once 20 21 #include "afxTempl.h" 22 23 class CIni 24 { 25 private: 26 CString m_strFileName; 27 public: 28 CIni(CString strFileName):m_strFileName(strFileName) 29 { 30 } 31 public: 32 //一般性操作: 33 BOOL SetFileName(LPCTSTR lpFileName); //設置文件名 34 CString GetFileName(void); //獲得文件名 35 BOOL SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate=true); //設置鍵值,bCreate是指段名及鍵名未存在時,是否創建。 36 CString GetValue(LPCTSTR lpSection, LPCTSTR lpKey); //得到鍵值. 37 BOOL DelSection(LPCTSTR strSection); //刪除段名 38 BOOL DelKey(LPCTSTR lpSection, LPCTSTR lpKey); //刪除鍵名 39 40 41 public: 42 //高級操作: 43 int GetSections(CStringArray& arrSection); //枚舉出全部的段名 44 int GetKeyValues(CStringArray& arrKey,CStringArray& arrValue,LPCTSTR lpSection); //枚舉出一段內的全部鍵名及值 45 46 BOOL DelAllSections(); 47 48 };
文件 2:
1 #include "StdAfx.h" 2 #include "ini.h" 3 4 #define MAX_ALLSECTIONS 2048 //全部的段名 5 #define MAX_SECTION 260 //一個段名長度 6 #define MAX_ALLKEYS 6000 //全部的鍵名 7 #define MAX_KEY 260 //一個鍵名長度 8 9 BOOL CIni::SetFileName(LPCTSTR lpFileName) 10 { 11 CFile file; 12 CFileStatus status; 13 14 if(!file.GetStatus(lpFileName,status)) 15 return TRUE; 16 17 m_strFileName=lpFileName; 18 return FALSE; 19 } 20 21 CString CIni::GetFileName(void) 22 { 23 return m_strFileName; 24 } 25 26 BOOL CIni::SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate) 27 { 28 TCHAR lpTemp[MAX_PATH] ={0}; 29 30 //以下if語句表示如果設置bCreate為false時,當沒有這個鍵名時則返回TRUE(表示出錯) 31 //!*&*none-value*&!* 這是個垃圾字符沒有特別意義,這樣亂寫是防止湊巧相同。 32 if (!bCreate) 33 { 34 GetPrivateProfileString(lpSection,lpKey,"!*&*none-value*&!*",lpTemp,MAX_PATH,m_strFileName); 35 if(strcmp(lpTemp,"!*&*none-value*&!*")==0) 36 return TRUE; 37 } 38 39 if(WritePrivateProfileString(lpSection,lpKey,lpValue,m_strFileName)) 40 return FALSE; 41 else 42 return GetLastError(); 43 } 44 45 CString CIni::GetValue(LPCTSTR lpSection, LPCTSTR lpKey) 46 { 47 DWORD dValue; 48 TCHAR lpValue[MAX_PATH] ={0}; 49 50 dValue=GetPrivateProfileString(lpSection,lpKey,"",lpValue,MAX_PATH,m_strFileName); 51 return lpValue; 52 } 53 54 BOOL CIni::DelSection(LPCTSTR lpSection) 55 { 56 if(WritePrivateProfileString(lpSection,NULL,NULL,m_strFileName)) 57 return FALSE; 58 else 59 return GetLastError(); 60 } 61 62 BOOL CIni::DelKey(LPCTSTR lpSection, LPCTSTR lpKey) 63 { 64 if(WritePrivateProfileString(lpSection,lpKey,NULL,m_strFileName)) 65 return FALSE; 66 else 67 return GetLastError(); 68 } 69 70 71 int CIni::GetSections(CStringArray& arrSection) 72 { 73 /* 74 本函數基礎: 75 GetPrivateProfileSectionNames - 從 ini 文件中獲得 Section 的名稱 76 如果 ini 中有兩個 Section: [sec1] 和 [sec2],則返回的是 ‘sec1‘,0,‘sec2‘,0,0 ,當你不知道 77 ini 中有哪些 section 的時候可以用這個 api 來獲取名稱 78 */ 79 int i; 80 int iPos=0; 81 int iMaxCount; 82 TCHAR chSectionNames[MAX_ALLSECTIONS]={0}; //總的提出來的字符串 83 TCHAR chSection[MAX_SECTION]={0}; //存放一個段名。 84 GetPrivateProfileSectionNames(chSectionNames,MAX_ALLSECTIONS,m_strFileName); 85 86 //以下循環,截斷到兩個連續的0 87 for(i=0;i<MAX_ALLSECTIONS;i++) 88 { 89 if (chSectionNames[i]==0) 90 if (chSectionNames[i]==chSectionNames[i+1]) 91 break; 92 } 93 94 iMaxCount=i+1; //要多一個0號元素。即找出全部字符串的結束部分。 95 arrSection.RemoveAll();//清空原數組 96 97 for(i=0;i<iMaxCount;i++) 98 { 99 chSection[iPos++]=chSectionNames[i]; 100 if(chSectionNames[i]==0) 101 { 102 arrSection.Add(chSection); 103 memset(chSection,0,MAX_SECTION); 104 iPos=0; 105 } 106 107 } 108 109 return (int)arrSection.GetSize(); 110 } 111 112 int CIni::GetKeyValues(CStringArray& arrKey,CStringArray& arrValue, LPCTSTR lpSection) 113 { 114 /* 115 本函數基礎: 116 GetPrivateProfileSection- 從 ini 文件中獲得一個Section的全部鍵名及值名 117 如果ini中有一個段,其下有 "段1=值1" "段2=值2",則返回的是 ‘段1=值1‘,0,‘段2=值2‘,0,0 ,當你不知道 118 獲得一個段中的所有鍵及值可以用這個。 119 */ 120 int i; 121 int iPos=0; 122 CString strKeyValue; 123 int iMaxCount; 124 TCHAR chKeyNames[MAX_ALLKEYS]={0}; //總的提出來的字符串 125 TCHAR chKey[MAX_KEY]={0}; //提出來的一個鍵名 126 127 GetPrivateProfileSection(lpSection,chKeyNames,MAX_ALLKEYS,m_strFileName); 128 129 for(i=0;i<MAX_ALLKEYS;i++) 130 { 131 if (chKeyNames[i]==0) 132 if (chKeyNames[i]==chKeyNames[i+1]) 133 break; 134 } 135 136 iMaxCount=i+1; //要多一個0號元素。即找出全部字符串的結束部分。 137 arrKey.RemoveAll();//清空原數組 138 arrValue.RemoveAll(); 139 140 for(i=0;i<iMaxCount;i++) 141 { 142 chKey[iPos++]=chKeyNames[i]; 143 if(chKeyNames[i]==0) 144 { 145 strKeyValue=chKey; 146 arrKey.Add(strKeyValue.Left(strKeyValue.Find("="))); 147 arrValue.Add(strKeyValue.Mid(strKeyValue.Find("=")+1)); 148 memset(chKey,0,MAX_KEY); 149 iPos=0; 150 } 151 152 } 153 154 return (int)arrKey.GetSize(); 155 } 156 157 BOOL CIni::DelAllSections() 158 { 159 int nSection; 160 CStringArray arrSection; 161 nSection=GetSections(arrSection); 162 for(int i=0;i<nSection;i++) 163 { 164 if(DelSection(arrSection[i])) 165 return GetLastError(); 166 } 167 return FALSE; 168 } 169 170 171 使用方法: 172 CIni ini("c:\\a.ini"); 173 int n; 174 175 /*獲得值 176 TRACE("%s",ini.GetValue("段1","鍵1")); 177 */ 178 179 /*添加值 180 ini.SetValue("自定義段","鍵1","值"); 181 ini.SetValue("自定義段2","鍵1","值",false); 182 */ 183 184 /*枚舉全部段名 185 CStringArray arrSection; 186 n=ini.GetSections(arrSection); 187 for(int i=0;i<n;i++) 188 TRACE("%s\n",arrSection[i]); 189 */ 190 191 /*枚舉全部鍵名及值 192 CStringArray arrKey,arrValue; 193 n=ini.GetKeyValues(arrKey,arrValue,"段1"); 194 for(int i=0;i<n;i++) 195 TRACE("鍵:%s\n值:%s\n",arrKey[i],arrValue[i]); 196 */ 197 198 /*刪除鍵值 199 ini.DelKey("段1","鍵1"); 200 */ 201 202 /*刪除段 203 ini.DelSection("段1"); 204 */ 205 206 /*刪除全部 207 ini.DelAllSections(); 208 */
***************************************************************************
××××××××××第二、VC有關文件操作,參考資料2
***************************************************************************
隨著Windows 2000和XP的普及,現在的大文件越來越多,而VC6中MFC的CFile類只支持不大於4GB的文件, 原因在於CFile類中使用了32位整型來處理文件,32位數的範圍是2的32次方(4GB),超過這個範圍的文件CFile就管不了,微軟.Net中VC7的CFile類支持大於4GB的文件,而.Net還不普及,開發桌面應用VC6還是首選,所以我們可以參照VC7寫一個CFile的繼承類CFile64,使它支持大於4GB的文件:
1.頭文件 File64.h
1 class CFile64 : public CFile 2 { 3 public: 4 5 // Attributes 6 ULONGLONG GetPosition(); 7 8 9 // Overridables 10 11 virtual ULONGLONG Seek(LONGLONG lOff, UINT nFrom); 12 virtual void SetLength(ULONGLONG dwNewLen); 13 ULONGLONG GetLength() ; 14 15 virtual void LockRange(ULONGLONG dwPos, ULONGLONG dwCount); 16 virtual void UnlockRange(ULONGLONG dwPos, ULONGLONG dwCount); 17 18 };
2.源文件 File64.cpp
1 #include "stdafx.h" 2 #include "file64.h" 3 4 //////////////////////////////////////////////////////////////////////////// 5 // CFile64 implementation 6 7 8 ULONGLONG CFile64::Seek(LONGLONG lOff, UINT nFrom) 9 { 10 ASSERT_VALID(this); 11 ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE); 12 ASSERT(nFrom == begin || nFrom == end || nFrom == current); 13 ASSERT(begin == FILE_BEGIN && end == FILE_END && current == FILE_CURRENT); 14 15 LARGE_INTEGER liOff; 16 17 liOff.QuadPart = lOff; 18 liOff.LowPart = ::SetFilePointer((HANDLE)m_hFile, liOff.LowPart, &liOff.HighPart, 19 (DWORD)nFrom); 20 if (liOff.LowPart == (DWORD)-1) 21 if (::GetLastError() != NO_ERROR) 22 CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName); 23 24 return liOff.QuadPart; 25 } 26 27 ULONGLONG CFile64::GetPosition() 28 { 29 ASSERT_VALID(this); 30 ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE); 31 32 LARGE_INTEGER liPos; 33 liPos.QuadPart = 0; 34 liPos.LowPart = ::SetFilePointer((HANDLE)m_hFile, liPos.LowPart, &liPos.HighPart , FILE_CURRENT); 35 if (liPos.LowPart == (DWORD)-1) 36 if (::GetLastError() != NO_ERROR) 37 CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName); 38 39 return liPos.QuadPart; 40 } 41 42 void CFile64::LockRange(ULONGLONG dwPos, ULONGLONG dwCount) 43 { 44 ASSERT_VALID(this); 45 ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE); 46 47 ULARGE_INTEGER liPos; 48 ULARGE_INTEGER liCount; 49 50 liPos.QuadPart = dwPos; 51 liCount.QuadPart = dwCount; 52 if (!::LockFile((HANDLE)m_hFile, liPos.LowPart, liPos.HighPart, liCount.LowPart, 53 liCount.HighPart)) 54 { 55 CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName); 56 } 57 } 58 59 void CFile64::UnlockRange(ULONGLONG dwPos, ULONGLONG dwCount) 60 { 61 ASSERT_VALID(this); 62 ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE); 63 64 ULARGE_INTEGER liPos; 65 ULARGE_INTEGER liCount; 66 67 liPos.QuadPart = dwPos; 68 liCount.QuadPart = dwCount; 69 if (!::UnlockFile((HANDLE)m_hFile, liPos.LowPart, liPos.HighPart, liCount.LowPart, 70 liCount.HighPart)) 71 { 72 CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName); 73 } 74 } 75 76 void CFile64::SetLength(ULONGLONG dwNewLen) 77 { 78 ASSERT_VALID(this); 79 ASSERT((HANDLE)m_hFile != INVALID_HANDLE_VALUE); 80 81 Seek(dwNewLen, (UINT)begin); 82 83 if (!::SetEndOfFile((HANDLE)m_hFile)) 84 CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName); 85 } 86 87 ULONGLONG CFile64::GetLength() 88 { 89 ASSERT_VALID(this); 90 91 ULARGE_INTEGER liSize; 92 liSize.LowPart = ::GetFileSize((HANDLE)m_hFile, &liSize.HighPart); 93 if (liSize.LowPart == (DWORD)-1) 94 if (::GetLastError() != NO_ERROR) 95 CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName); 96 97 return liSize.QuadPart; 98 }
/////////////////////////////////////////////////////////////////////////////
LONGLONG是64位整型,這樣在理論上可支持的最大文件為18000000000GB,你也可以根據自己的需要重載CFile的其他函數
整理自:http://www.cnblogs.com/lidabo/p/3470085.html
VC++中文件操作(二)--- .ini文件、CFile64