Logging data via INI configuration file

A window, when the user wants to retain the data, of course, we can store the data through the database, but when the data is small, we can often store the initialization information of the window through the configuration file, and the storage efficiency is high. The code for reading and writing INI files is given below:

Here we write our own INI file, we must first know the storage path of the INI file.

The INI file should be placed in the same file directory as the EXE, so that no matter how the location of the EXE changes, the INI file can be changed, and the saved information will not be lost because the INI file cannot be found.

Step 1: Get the EXE file directory (please refer here, my blog on getting the EXE file directory)

Step 2: Write the INI file. When we get g_exePATH, we can start reading and writing INI files. (g_exePATH below is EXE execution path + "\\XRayInfo.ini")

Writing to the filesystem gives us the interface:

::WritePrivateProfileSection

::WritePrivateProfileString

WritePrivateProfileStruct

Let's take a look at their definitions:



Here we use the simplest ::WritePrivateProfileString to write data into the INI file

::WritePrivateProfileString(_T("XRayInfo"),_T("port"),port,g_exePATH);

::WritePrivateProfileString(_T("XRayInfo"),_T("totaltime"),totaltime,g_exePATH);

::WritePrivateProfileString(_T("XRayInfo"),_T("waittime"),waittime,g_exePATH);

::WritePrivateProfileString(_T("XRayInfo"),_T("xraystatus"),xraystatus,g_exePATH);

::WritePrivateProfileString(_T("XRayInfo"),_T("voltage"),voltage,g_exePATH);

::WritePrivateProfileString(_T("XRayInfo"),_T("current"),current,g_exePATH);


Step 3: Read the INI file

When the MFC window is initialized, we read the INI file and can set the initial value for the INI file

Read the INI file, the system also gives us different interfaces

::GetPrivateProfileInt

::GetPrivateProfileSection

::GetPrivateProfileString

::GetPrivateProfileStruct








Here we use the data of type Int as an example:

int  port = ::GetPrivateProfileInt("XRayInfo","port",0,g_exePATH);

Finally: pass, get the data, and assign a value to the control of the window, it is OK



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769474&siteId=291194637