jasmine214--love

          只有當你的內心總是充滿快樂、美好的愿望和寧靜時,你才能擁有強壯的體魄和明朗、快樂或者寧靜的面容。
          posts - 731, comments - 60, trackbacks - 0, articles - 0

          VC--ini文件-配置信息--函數用法

          Posted on 2010-08-14 17:08 幻海藍夢 閱讀(522) 評論(0)  編輯  收藏 所屬分類: C++
           
          http://blog.pfan.cn/sword2008/31089.html
           

          配置文件中經常用到ini文件,在VC中其函數分別為:

          寫入.ini文件:bool WritePrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpString,LPCTSTR lpFileName);

          讀取.ini文件:DWORD GetPrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpDefaut,LPSTR lpReturnedString,DWORD nSize,LPCTSTR lpFileName);

          讀取整形值:UINT GetPrivateProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName,INT nDefault,LPCTSTR lpFileName);

          其中個參數的意思:

          LPCTSTR lpAppName ------- INI文件中的一個字段名

          LPCTSTR lpKeyName -------- lpAppName 下的一個鍵名,也就是里面具體的變量名

          LPCTSTR lpString ---------是鍵值,也就是變量的值, 必須為LPCTSTRCString類型

          LPCTSTR lpFileName --------完整的INI文件路徑名

          LPCTSTR lpDefaut ----------如果沒有其前兩個參數值,則將此值賦給變量

          LPSTR lpReturnedString --------接收INI文件中的值的CString對象,即接收緩沖區

          DWORD nSize ------接收緩沖區的大小

          例子:

          CString StrName,Strtemp;

          int nAge;

          StrName = "jacky";

          nAge = 13;

          WritePrivateProfileString("Student","Name",StrName,"c:\\setting.ini");

          結果:(INI文件中顯示如下:)

          [Student]

          Name=jacky

          讀取:

          CString SName;

          GetPrivateProfileString("Student","Name","DefaultName",SName.GetBuffer(MAX_LENGTH),MAX_LENGTH,"c:\\setting.ini");

          結果:SName = "jacky";這里需要注意點就是用完GetBuffer函數后一定要釋放(SName.ReleaseBuffer()函數),不然后面再用到SName的其他子函數就會失靈。

          讀整數比較簡單,如下

          int Result = GetPrivateProfileInt("Student","nAge",0,"c:\\setting.ini")返回值即為所讀取的結果!

          GetPrivateProfileString最后一個參數是配置文件路徑的參數,此路徑只能是絕對路徑,不能是相對路徑,但現在我需要是我的exe文件能和我的配置文件在一起。因此我使用了GetCurrentDirectory函數。

          原代碼如下:

          CString server_ip;
          CString des="";
          ::GetCurrentDirectory(MAX_PATHLENGTH,des.GetBuffer(MAX_PATHLENGTH));
          des.ReleaseBuffer();
          des+="
          \\config.ini";
          GetPrivateProfileString("PhoneDemo","Server_IP","",server_ip.GetBufferSetLength(15),15,des);
          server_ip.ReleaseBuffer();

          注意:在這里使用CString變量時,在使用完GetBuffer后,緊接著一定要使用ReleaseBuffer()函數,才可以進行其他的諸如字符串+操作

           


          例如:
           

          [settings]

          remember_check=0
          language_sel=0
          IP_adress=192.168.1.1
          Password_sel=098764
          usr_name_sel=opipiio


          在我們寫的程序當中,總有一些配置信息需要保存下來,以便完成程序的功能,最簡單的辦法就是將這些信息寫入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="
          張三";
          nAge=12;
          ::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;
          文件://共有6個文件名需要保存
          for(i=0;i {strTemp.format("%d",i);
          strTempA=
          文件名;
          文件://文件名可以從數組,列表框等處取得
          .
          ::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
          "c:\\usefile\\usefile.ini");
          }
          strTemp.format("%d",nCount);
          ::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");
          文件://將文件總數寫入,以便讀出.

            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");

          文件://使用strTempA中的內容.

          }

            補充四點:

             1.INI文件的路徑必須完整,文件名前面的各級目錄必須存在,否則寫入不成功,該函數返回 FALSE .

             2.文件名的路徑中必須為 \\ ,因為在VC++, \\ 才表示一個 \ .

             3.也可將INI文件放在程序所在目錄,此時 lpFileName 參數為: ".\\student.ini".

             4.從網頁中粘貼源代碼時,最好先粘貼至記事本中,再往VC中粘貼,否則易造成編譯錯誤,開始時我也十分不解,好好的代碼怎么就不對呢?后來才找到這個方法.還有一些代碼中使用了全角字符如:<,\等,也會
          造成編譯錯誤.

          主站蜘蛛池模板: 延安市| 吕梁市| 奉贤区| 子洲县| 寿阳县| 丹棱县| 手游| 和林格尔县| 马鞍山市| 衡阳县| 安塞县| 达日县| 海晏县| 通江县| 儋州市| 镇平县| 江阴市| 家居| 新源县| 江永县| 阿拉善左旗| 桦川县| 屏东市| 遂川县| 昌吉市| 正安县| 原平市| 嘉义县| 石城县| 喀喇| 曲阜市| 拉萨市| 武乡县| 广丰县| 马山县| 陕西省| 南陵县| 丰县| 和林格尔县| 淮安市| 乌兰浩特市|