Operate Ini file

xiaoxiao2021-03-05  22

Overview

The set or other small amount of data is often used in the program, so that the program can be used when executed next time, such as the position, size, the data of the window, some users set, etc., under DOS When programming, we generally generate a file, write this data to the file, and then read it again in the next execution. In Win32 programming, you can also do this, but Windows has provided us with two convenient methods, which is to save a small amount of data using the registry or INI file (Profile). This article first introduces the use of .ini files.

The INI file is a text file, and the middle data format is generally:

[Section1 name]

Keyname1 = Value1

Keyname2 = Value2

...

[Section2 name]

Keyname1 = Value1

Keyname2 = Value2

Ini files can be divided into several sections. The name of each section is enclosed in [], in a section, there can be many Key, each key can have a value and occupy a line, the format is key = value, Win32 pair A portion of the INI file operation is a part of the Win.ini operation, part of the user-defined INI file operation. Win.IN and System.ini are two very important initialization files of Windows, and Windows records the selection made by the user and various changes in the system information in these two files. System.ini describes the current state of the system hardware, and the Win.ini file contains the current configuration of the Windows system running environment. Due to the importance and commonality of the Win.ini file, Win32 has an API that operates for Win.ini, which is:

GetProfileint - gets a key value of a key from a section of the win.ini file, its original shape is:

GetProfileint

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPCTSTSTR LPKEYNAME, / / ​​Point to the string address containing the KEY name

INT ndefault // If the key value is not found, the default value is it?

);

If the key value is not found, the return value is the default value specified by nDefault. If the value in the key is negative, return 0, if the key specifies the mixing of the numbers and strings, the value of the number portion is returned, such as x = 1234ABCD, return 1234

GetProfileString - gets a key string from a section of the win.ini file, its original shape:

GetProfileString

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPCTSTSTR LPKEYNAME, / / ​​Point to the string address containing the KEY name

LPCTSTR LPDEFAULT, // If the key value is not found, return the user of the default string

LPTSTR LPRETURNEDSTRING, / / ​​Returns the buffer address of the string

DWORD NSIZE / / Buffer length

);

The returned string is in the buffer, the returned EAX value is the length of the returned string (not including the end of the tail)

GetProfileSECTION - Reads the entire section from the win.ini file, its original shape is: getProfilesection

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPTSTR LPRETURNEDSTRING, / / ​​Return to data buffer addresses

DWORD nsize // Return to data buffer length

);

WriteProfileSection - Write the value of a whole section to the specified section of the Win.ini file, its original shape is:

WriteprofileeSecion

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPCTSTR LPSTRING / / The address of the data to be written

);

If Win.ini does not have a specified section, the API will create a newly established data. If there is already existing, first delete all the key in the original SECTON is new.

WriteProfileString - Write a key value to the specified section of the Win.ini file, its original shape is:

WriteprofileString

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPCTSTSTR LPKEYNAME, / / ​​Point to the string address containing the KEY name

LPCTSTR LPSTRING / / The string address to be written

);

If Win.ini does not specify, the API will create a new section. If there is no specified key, create a new key and write data, if it already exists, use a string instead of the original value.

The above API is working for Win.ini. Of course, for us, more is to build your own INI file in the directory running, if you need to operate your own INI file, you will use another Group API, this set of APIs and tops, as long as the above set of Profile can be replaced with privateprofile (private), the parameters of an INI file name are also corresponding to the parameters. For example, GetPrivateProfileint, GetPrivateProfileSECing, WritePrivateProfileSTRING, etc.

GetPrivateProfileint - gets a key value from a section from the INI file, its original shape is:

GetPrivateProfileint

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPCTSTSTR LPKEYNAME, / / ​​Point to the string address containing the KEY name

INT ndefault // If the key value is not found, the default value is it?

LPCTSTSTR LPFILENAME / / INI file name

);

The definition of intermediate parameters and return values ​​is the same.

GetPrivateProfileString - gets a key string from a section of the INI file, its original shape:

GetPrivateProfileString

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPCTSTSTR LPKEYNAME, / / ​​Point to the string address containing the KEY name

LPCTSTR LPDEFAULT, // If the key value is not found, return the user of the default string

LPTSTR LPRETURNEDSTRING, / / ​​Return to the buffer address of the string DWORD NSIZE / / Buffer length

LPCTSTSTR LPFILENAME / / INI file name

);

GetPrivateProfileeration - Reads the entire section from the INI file, its original shape is:

GetPrivateProfileeSecion

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPTSTR LPRETURNEDSTRING, / / ​​Return to data buffer addresses

DWORD nsize // Return to data buffer length

LPCTSTSTR LPFILENAME / / INI file name

);

This API can read the entire section content, when you don't know which Key in section, you can use this API to read the entire section and then processed.

GetPrivateProfileeSectionNames - gets the name of the section from the INI file, its original shape is:

GetPrivateProfileeSectionNames

LPTSTR LPSZRETURNBuffer, // Return to the buffer address of the data

DWORD nsize // Return to data buffer length

LPCTSTSTR LPFILENAME / / INI file name

);

If there are two sections: [Sec1] and [SEC2] in INI, the return is 'Sec1', 0, 'SEC2', 0, 0, when you don't know what sections in INI, you can use this API. Get the name

WritePrivateProfileeSection - Initize the content of the entire section into the INI file specified in section, its original shape is:

WritePrivateProfileSecion

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPCTSTR LPSTRING / / The address of the data to be written

LPCTSTSTR LPFILENAME / / INI file name

);

WritePrivateProfileString - Write a Key value to the specified section of the INI file, its original shape is:

WriteprivateProfileString

LPCTSTR LPAPPNAME, / / ​​Point to the string address containing the section name

LPCTSTSTR LPKEYNAME, / / ​​Point to the string address containing the KEY name

LPCTSTR LPSTRING / / The string address to be written

LPCTSTSTR LPFILENAME / / INI file name

);

If there is no specified section in INI, the API will create a new section. If there is no specified key, create a new key and write data. If it already exists, use a string instead of the original value. When the specified INI does not exist, the API automatically creates a new file, so the benefit of using INI is that we don't have to save a small amount of data involved in file operations, even if you don't have the operation where you exist if you find the file.

Use points:

When we do actually use, you use getPrivateProfileString and WritePrivateProfileString, but when you operate on custom INI files, if there is no path specified by LPFileName, the API will go to Windows installation directory to find Will not find a current directory, but every time II functions should be used to get the current path. It is obviously too trouble. Here is a variety of ways, you only need to add it in front of the INI file name. / Other, such as this directory User.ini operation below, then the file name is './user.ini' This is obviously more convenient. In addition, when you want to clear a key clear, you can use the lpstring to point to an empty string and use WritePrivateProfileString. When you want to clear all the contents of a section, you don't have to clear your KEY one by one, you can use the lpstring to point to an empty string and then use WritePrivateProfilesection. A class that operates the INI file:

.h

// InIfile.h: interface for the cinireader class.

//

//

#if! defined (afX_INIFILE_H__999976B4B_DBA1_4D1E_AA14_CBEB63042FD1__included_)

#define AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__included_

#iF _MSC_VER> 1000

#pragma overce

#ENDIF / / 100 m _ _ _

#include

Class Cinireader

{

PUBLIC:

//Method to set ini file name, if not already specified

Void setiniFileName (CString strinifile);

//Methods to return the list of section data and section names

CStringList * getsectionData (cstring strsection);

CStringList * getsectionNames ();

// Check if the section exists in the file

Bool SectionExists (CString StRSEC);

// Updates the key value, if key already exists, Else Creates a Key-Value PAIR

Long SetKey (CSTRING STRKEY, CSTRING STRKEY, CSTRING STRSECTION);

// give the key value for the specified key of a section

CString getKeyValue (CString strkey, cstring strsection);

// Default Constructionor

Cinireader ()

{

m_sectionList = new cstringlist ();

m_sectionDataList = new cstringlist ();

}

Cinireader (CString Strfile)

{

m_strfilename = STRFILE;

m_sectionList = new cstringlist ();

m_sectionDataList = new cstringlist ();

~ Cinireader ()

{

DELETE M_SECTIONLIST;

DELETE M_SECTIONDATALIST;

}

Private:

// lists to keep seats and section data

CSTRINGLIST * m_SECTIONDATALIST;

CSTRINGLIST * m_SECTIONLIST;

CSTRING M_STRSECTION;

Long M_LRetValue;

// ini file name

CString M_StrFileName;

}

#ndif //! defined (AFX_INIFILE_H__999976B4B_DBA1_4D1E_AA14_CBEB63042FD1__included_)

.cpp

// InIfile.cpp: Implementation of the cinireader class.

//

//

#include "stdafx.h"

// # include "readini.h"

#include "ini.h"

#include

#ifdef _Debug

#undef this_file

Static char this_file [] = __ file__;

#define new debug_new

#ENDIF

//

// construction / destruction

//

// used to retrieve a value give the section and key

CString Cinireader :: GetKeyValue (CString Strkey)

{

CHAR AC_RESULT [255];

// Get the info from the .ini file

M_LRetValue = getPrivateProfileString ((lpctstr) strsection, (lpctstr) strkey,

"", ac_result, 255, (lpctstr) m_strfilename);

CSTRING STRESULT (Ac_Result);

Return Strresult;

}

// used to add or set a key value pair to a section

Long Cinireader :: SetKey (CSTRING STRVALUE, CSTRING STRKEY, CSTRING STRING STRSECTION)

{

M_LRetValue = WritePrivateProfileString (STRSECTION, StrKey,

Strvalue, m_strfilename);

Return M_LRetValue;

}

// use to find outness Given Section EXISTS

Bool Cinireader :: SectionExists (CString StRSECTION)

{

CHAR AC_RESULT [100];

Cstring csaux;

// Get the info from the .ini file

M_LRetValue = getPrivateProfileString ((LPCTSTR) STRSECTION, NULL,

"", ac_result, 90, (lpctstr) m_strfilename);

// Return if We Could Retrieve Any Info from That Section

Return (M_LRetValue> 0);

}

// used to retrieve all of the section in the ini filecstringlist * cinireader :: getsectionnames () // Returns Collection of Section Names

{

CHAR AC_RESULT [2000];

m_sectionList-> removeall ();

M_LRetValue = getPrivateProfileeSectionNames (ac_result, 2000, (lpctstr) m_strfilename);

Cstring strsectionname;

For (int i = 0; i

{

IF (AC_RESULT [I]! = '/ 0') {

STRSECTIONNAME = STRSECTIONNAME AC_RESULT [i];

} else {

IF (strSECTIONNAME! = ") {

m_sectionList-> insertafter (m_sectionList-> gettailposition (), strsECTIONNAME);

}

STRSECTIONNAME = ""

}

}

Return M_SECTIONLIST;

}

// Used to Retrieve All Key / Value Pairs of a Given Section.

CSTRINGLIST * CINIREADER :: getsectionData (cstring strsection)

{

CHAR AC_RESULT [2000]; // Change Size Depending on Needs

m_sectionDataList-> removall ();

M_LRETVALUE = GetPrivateProfileSECTION ((LPCTSTR) STRSECTION, AC_RESULT, 2000, (LPCTSTSTR) m_strfilename);

Cstring strsectiondata;

For (int i = 0; i

{

IF (AC_RESULT [I]! = '/ 0') {

STRSECTIONDATA = STRSECTIONDATA AC_RESULT [I];

} else {

IF (strSECTIONDATA! = ") {

M_sectionDataList-> INSERTAFTER (m_sectionDataList-> gettailposition (), strsectionData);

}

STRSECTIONDATA = ""

}

}

Return m_sectionDataList;

}

Void Cinireader :: SetiniFileName (CString Strinifile)

{

m_strfilename = strinifile;

}

转载请注明原文地址:https://www.9cbs.com/read-37603.html

New Post(0)