A class that manually reads and writes the INI file (1)

zhaozj2021-02-16  59

In Windows, getPrivateProfileString and WritePrivateProfileString functions can read and write INI profiles, but each of these two functions have to open the file, search in the file, so that the efficiency of this will definitely be very slow, so one is provided below. Read the profile into memory, this is the benefit of reading a file, quickly search (using MAP map). All data can be saved in a string or file.

The INI profile is mainly composed of four parts: group, key value, content, comment, and blank line, one will give an example file.

File: E: /BOOT.INI

[boot loader]; here is a group, the following two line configuration data belongs to this group

Timeout = 1; here is a key value in front of the equal number, the next is one content

DEFAULT = MULTI (0) Disk (0) RDisk (0) partition (2) / Winnt; the following line is an empty line

[Operating systems]; all characters behind ';' belong to a comment, this program does not support REM-form annotations

Multi (0) DISK (0) RDisk (0) Partition (2) / Winnt = "Microsoft Windows 2000 Professional" / FastDetect; Sadfkl;

C: / = "Microsoft Windows"

Ok, I know the structure of the INI file, and start to analyze the data structure that the INI file reads in memory.

An INI file can be seen as consisting of some groups and the data below each group, and the group is a string form, and the data is a more complex object. For the convenience of search, CMAPSTRINGTOPTR is used to organize the entire INI file, so that the data in the group can be easily queried by the string of the group.

The data below a group is a mapping relationship consisting of some key value -à content, so it is best to use CMapStringTString to group this data.

The header file and implementation section of this class will be given below. Give it a brief introduction to the usage of this class:

Read the above E: /BOOT.INI file:

#include "cfgdata.h"

Ccfgdata cfgdata;

// load ini file

Cfgdata.loadcfgdata ("e: //boot.ini");

CString Str;

Long L = 0;

// Set the current group

Cfgdata.setgroup ("Boot Loader");

/ / Read Long type data to the variable L

Cfgdata.getlongData ("Timeout", L);

// Read the string type data to the variable STR

Cfgdata.getstrdata ("Default", STR);

// Set the current group

Cfgdata.SetGroup ("Operating Systems");

// Read the string type data to the variable STR

Cfgdata.getstrdata ("Multi (0) DISK (0) RDisk (0) Partition (2) // WinNT", STR);

// Read the string type data to the variable STR

Cfgdata.getstrdata ("C: //", STR);

/ / Save the entire configuration data into the string

Cfgdata.savetostr (& STR);

/ / Save the entire configuration data into the file, pay attention to the configuration data without the order relationship,

// So there may be a group of several key values ​​between a group ---> content pairing between groups and groups

// and previous inconsistency, all of the comments and empty lines are lost in cfgdata.savecfgdata.

"E: //boot2.ini"

);

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

New Post(0)