Article title: Operation of the INI file by Visual C : Www.5ivb.net: KYLL_BU Release Type: Reprint Release Date: 2004-09-03 Today Views: 3 Total View: 908
Among the procedures we wrote, there are always some configuration information that needs to be saved so that the functionality of the program is to write this information to the INI file. The program is initialized. The specific application is as follows: 1. WINAPI .INI file information writing function prototype 1 was used: BOOL WritePrivateProfileString (LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName); wherein the meaning of the parameters:. LPCTSTR lpAppName is a field name INI file. LPCTSTR LPKEYNAME is a key name under LPAppname. It is a variable name. LPCTSTR lpstring is a key value, that is, the value of the variable, but must be the LPCTSTR or CString type. LPCTSTR LPFileName is a complete INI file name. 2. Specific How to use: Set up a student, you need to write his name and age into the C: /Stud/student.ini file. CString Strname, Strtemp; int nage; strname = "Zhang 3"; NAGE = 12; :: WritePrivateProfileString ("StudentInfo", "Name", Strname, "C: //stud//student.ini"); this time in the c: /stud/student.ini file is as follows: [studentInfo] Name = Zhang 3 3 To save the age of the students, simply try the integer value to be characterized: strTemp.format ("% D", NAGE); :: WritePrivateProfileString ("StudentInfo", "Age", Strtemp, " C: //stud//student.ini "); II. Send information from the variable in the program from the INI file. 1. WinAPI function originals used in: DWORD getPrivateProfileString (LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTR LPDEFAULT, LPTSTR LPDEFAULT, LPTSTR LPRETURNEDSTRING, DWORD NSIZE, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTR LPFILENAME The meaning of each parameter: the first two parameters are the same as the meaning of WritePrivateProfileString. LPDefault: If there is no item name or key name specified in the INI file, this value is assigned to the variable. LPRETURNEDSTRING: Receive in INI file The value of the CString object, the purpose buffer. Nsize: The size of the destination buffer. LPFileName: is a complete INI file name. 2. Specific use: I now use the information read in the previous step in the previous step. . CString strStudName; int nStudAge; GetPrivateProfileString ( "StudentInfo", "name", "default name", strStudName.GetBuffer (MAX_PATH), MAX_PATH, "c: //stud//student.ini"); strStudName value after execution In order to: "Zhang San", if the first two parameters are incorrect, its value is: "Default Name". 3. Read the integer value to use another WinAPI function: uint getprivateprofileint (lpctstr lpappname, lpctstr lpkeyname, int NDefault, LPCTSTR LPFILENAME); the parameters here are the same. The usage method is as follows: nStudage = getPrivateProfileint ("
StudentInfo "," AGE ", 10," C: //stud/student.ini "); III. The loop writes multiple values, there is a program, to save the recently used file name, specifically The procedure is as follows: 1. Write: cstring straTemp, startpa; int i; int ncount = 6; file: // There are 6 file names need to save for (i = 0; i {straTemp.format ("% d", i ); strTempa = file name; file: // file name can be obtained from arrays, list boxes, etc. :: WritePrivateProfileString ("Usefilename", "FileName" Strtemp, Strtempa, "C: //Usefile//usefile.ini ");} strtemp.format ("% d ", ncount) ;: WritePrivateProfileString (" Filecount "," Count ", Strtemp," C: //Usefile//Usefile.ini "); file: // Put file The total number is written to read. 2. Read: 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.ini "); file: // uses content in strTempa.} Supplement four points: 1.ini files must be complete, the file names must exist in front of the file name, otherwise it is unsuccessful, This function returns a false value. 2. The file name must be //, because in VC , // represents one /. 3. You can also put the INI file in the program where the program is located. At this time, the LPFileName parameter is: " . // ---------------------------------------- ----------- ------------------------------- / * Category: CINI version: v2.0 last updated: V2.0 Dream Child Feeded on February 14, 2004 Valentine's Day Add to advanced operation V1.0 Dream Child's General Operation Class Description in 2003: This class can operate in .ini file * / file 1: #pragma overce #include " afxTempl.h "class CIni {private: CString m_strFileName; public: CIni (CString strFileName): m_strFileName (strFileName) {} public: // general operation: BOOL SetFileName (LPCTSTR lpFileName); // set the file name CString GetFileName (void ); // get the file name BOOL SetValue (LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue, bool bCreate = true); // set the key,
BCREATE is whether it is created when the indication and key name is not present. CString GetValue (LPCTSTR lpSection, LPCTSTR lpKey); // get key BOOL DelSection (LPCTSTR strSection); // delete the section name BOOL DelKey (LPCTSTR lpSection, LPCTSTR lpKey); // delete key name public:. // Advanced Operations: int GetSections (CStringArray & arrSection); // enumerate all of the section name int GetKeyValues (CStringArray & arrKey, CStringArray & arrValue, LPCTSTR lpSection); // enumerate all keys and values over a period BOOL DelAllSections ();}; file 2: #include "stdafx.h" #include "ini.h" #define max_allsections 2048 // all paradigm #define max_section 260 // a paragraph name length #define max_allkeys 6000 // All key name #define max_key 260 // the length of a key name BOOL CIni :: SetFileName (LPCTSTR lpFileName) {CFile file; CFileStatus status; if return TRUE (file.GetStatus (lpFileName, status)!); m_strFileName = lpFileName; return FALSE;} CString CIni :: GetFileName (void) {Return M_StrFileName;} BOOL CINI :: SetValue (LPCTSTR LPSECTION, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTER LPTEMP [MAX_PATH] = {0}; // The following IF statement means that if bcreate is false, When there is no such key name, return True (indicating an error) //! * & * None-value * &! * This is no special meaning, so the words are to prevent the same.
if (! bcreate) {GetPrivateProfileString (LPSECTION, LPKEY, "! * & * None-value * &! *", lptemp, max_path, m_strfilename); if (strcmp, "! * & * none-value * &! * ") == 0) return TRUE;} if (WritePrivateProfileString (lpSection, lpKey, lpValue, m_strFileName)) return FALSE; else return GetLastError ();} CString CIni :: GetValue (LPCTSTR lpSection, LPCTSTR lpKey) {DWORD dValue; TCHAR lpValue [MAX_PATH] = {0}; dValue = GetPrivateProfileString (lpSection, lpKey, "", lpValue, MAX_PATH, m_strFileName); return lpValue;} BOOL CIni :: DelSection (LPCTSTR lpSection) {if (WritePrivateProfileString (lpSection, NULL, NULL, m_strFileName)) return FALSE; else return GetLastError ();} BOOL CIni :: DelKey (LPCTSTR lpSection, LPCTSTR lpKey) {if (WritePrivateProfileString (lpSection, lpKey, NULL, m_strFileName)) return FALSE; else return GetLastError (); } int CINI :: GetSections (CStringArray & ArrSection) {/ * This function is foundation: getPrivateProfileeSectionNames - Name of the section from the INI file If there are two sections: [SEC1] and [SEC2] in INI, return 'sec1' , 0, 'SEC2', 0, 0, when you don't know ini When you have this api, you can use this API to get the name * / int i; int ipos = 0; int imaxcount; tchar chsectionnames [max_allsections] = {0}; // Total bricled string tchar chsection [max_section] = {0}; // Store a paragraph name. GetPrivateProfileeSectionNames (chSECTIONNAMES, MAX_ALLSES, M_STRFILENAME); // The following loop, cut to two consecutive 0 for (i = 0; i Arrsion.removeall (); // Empty the original number group for (i = 0; i