古詩詞大全網 - 四字成語 - MFC中要如何保存和提取自己程序專用的數據文件?

MFC中要如何保存和提取自己程序專用的數據文件?

可以用windows api寫入ini, 然後用windows api讀取。

WritePrivateProfileStruc

GetPrivateProfileStruct

這兩個很方便。

/en-us/library/ms725502(VS.85).aspx

用API寫INI文件的函數有

BOOL WritePrivateProfileString(

LPCTSTR lpAppName, // 節名

LPCTSTR lpKeyName, // 鍵名

LPCTSTR lpString, // 添加的字符串

LPCTSTR lpFileName // Ini文件名

);

BOOL WritePrivateProfileStruct(

LPCTSTR lpszSection, // pointer to section name

LPCTSTR lpszKey, // pointer to key name

LPVOID lpStruct, // 要寫入的數據緩沖區

UINT uSizeStruct, // 緩沖區的大小

LPCTSTR szFile // pointer to initialization filename

);

BOOL WritePrivateProfileSection(

LPCTSTR lpAppName, // pointer to string with section name

LPCTSTR lpString, // 寫入的字符串

LPCTSTR lpFileName // pointer to string with filename

);

用API讀INI文件的函數有

DWORD GetPrivateProfileString(

LPCTSTR lpAppName, // points to section name

LPCTSTR lpKeyName, // points to key name

LPCTSTR lpDefault, // 默認字符串 ,如果沒有則返回該值

LPTSTR lpReturnedString, // 返回的字符串

DWORD nSize, // 返回字符串的大小

LPCTSTR lpFileName // points to initialization filename

);

DWORD GetPrivateProfileSection(

LPCTSTR lpAppName, // address of section name

LPTSTR lpReturnedString, // address of return buffer

DWORD nSize, // size of return buffer

LPCTSTR lpFileName // address of initialization filename

);

UINT GetPrivateProfileInt(

LPCTSTR lpAppName, // address of section name

LPCTSTR lpKeyName, // address of key name

INT nDefault, // return value if key name is not found

LPCTSTR lpFileName // address of initialization filename

);

BOOL GetPrivateProfileStruct(

LPCTSTR lpszSection, // address of section name

LPCTSTR lpszKey, // address of key name

LPVOID lpStruct, // address of return buffer

UINT uSizeStruct, // size of return buffer

LPCTSTR szFile // address of initialization filename

);

DWORD GetPrivateProfileSectionNames(

LPTSTR lpszReturnBuffer, // address of return buffer

DWORD nSize, // size of return buffer

LPCTSTR lpFileName // address of initialization filename

);

當然還有如WriteProfileString,WriteProfileSection,WriteProfileSturct, GetProfileString,GetProfileStruct,GetProfileSectionNames,GetProfileInt,GetProfileSection但這些只對Win.ini有效

下面我們來學習它們的用法

WritePrivateProfileString函數是向ini文件中寫入字符串,如

WritePrivateProfileString(Pchar('類型'),Pchar('API'),Pchar('API 真好!'),Pchar('c:\example.ini'));

如果第二個參數是nil,那麽該操作將刪除該節

如果第三個參數為nil,那麽該操作將刪除該節中的所有鍵

如果在指定的文件中沒有路徑,那麽它將在系統的目錄尋找文件,如果不存在則建立

WritePrivateProfileSection是向文件中寫入壹整個鍵,其它鍵的格式為key = value,如

WritePrivateProfileSection(Pchar('類型'),Pchar('其它=123'),Pchar('c:\example.ini'));

註意,該操作將刪除該節中的所有鍵後在進行本次的寫入

WritePrivateProfileStruct是向文件中寫入壹個結構,如

type

TPerson = record

Name:string;

Age:integer;

end;

var

Per:TPerson;

WritePrivateProfileStruct(Pchar('類型'),Pchar('結構'),@Per,Sizeof(Per),Pchar('C:\example.ini'));

GetPrivateProfileString是從文件中讀出壹個指定鍵的字符串,如果沒有找到,則返回默認值

GetPrivateProfileString(Pchar(‘類型'),Pchar('API'),Pchar('no value'),Str1,21,Pchar('c:\example.ini'));

GetPrivateProfileSection是從文件中讀出壹整個鍵

GetprivateProfileSection('類型',str1,200,'c:\example.ini');

GetPrivateProfileInt是從文件中讀出指定鍵的整型值,如果沒找到或不是整型的值,則返回默認值,如果返回值小於0,則返回0,如

i:=GetPrivateProfileInt(Pchar('類型'),Pchar('整型'),i,Pchar('C:\example.ini'));

showMessage(inttostr(i));

GetPrivateProfileStruct從文件中讀出指定鍵的結構值,如

type

TPerson = record

Name:string;

Age:integer;

end;

var

Buffer:TPerson;

GetPrivateProfileStruct(Pchar('類型'),Pchar('結構'),@Buffer,Sizeof(Per),Pchar('C:\example.ini'));

GetPrivateProfileSectionNames是從文件中讀出所有節的名稱,該函數返回讀入緩沖區的字符數,如

count:=GetPrivateProfileSectionNames(Str3,200,Pchar('c:\example.ini'));

ShowMessage(Str3);

此處顯示的只是第壹個節的名稱,如果要顯示第二個字符的名稱則要用到下面這句

showmessage(str3+5);

這句不用多解釋吧?