labwindows/cvi 2019入门(3)——读写INI文件

应用程序经常需要保存一些的有用的信息等,如下次打开软件时要加载的数据,程序设定的基本工艺参数等等。INI文件是一种文本格式的文件,方便修改和配置。LabWindows/CVI中对INI文件的操作提供了丰富的接口函数,本文在阅读帮助文档的基础上,提供了一个最基本的例子程序,能够满足大多数使用。

ini文件格式:

ini文件是initialization file的缩写,其用于保存参数设置。一般我们会将UI配置的界面参数保存在ini文件中,在程序下次启动时,读取ini文件配置信息,这样就不用重复配置程序参数。
ini文件由键和节组成,如下所示:
键(keys):键是ini文件的基本元素,每一个键又由名称和值组成,如下所示:
name = value
节(sections):节是键的群组合,节由“[]”包围,如下所示:
[section]

LabwindowsCVI 对ini文件的支持:

LabwindowsCVI对ini操作的api如下所示:
Ini_New:
Ini_New用于创建一个IniText对象,其函数原型如下所示:
IniText Ini_New (int automaticSorting);
其中automaticSorting为ini文件中 键是否排序,非零值代表排序。

Ini_ReadFromFile:
Ini_ReadFromFile用于读取ini文件的内容,其函数原型如下所示:
int Ini_ReadFromFile (IniText handle, const char pathname[]);
其中handle为Ini_New返回的句柄,pathname为ini文件的路径。

Ini_GetInt:
Ini_GetInt用于读取整形键值,其函数原型如下所示:
int Ini_GetInt (IniText handle, const char sectionName[], const char tagName[], int *integerValue);
其中handle为IniText句柄,sectionName为节的名称,tagName为键的名称,integerValue为读取的键值。

Ini_GetDouble:
Ini_GetDouble用于读取双浮点型键值,其函数原型如下所示:
int Ini_GetDouble (IniText handle, const char sectionName[], const char tagName[], double *doubleValue);
其中handle为IniText句柄,sectionName为节的名称,tagName为键的名称,doubleValue为读取的键值。

Ini_GetStringIntoBuffer:
Ini_GetStringIntoBuffer用于读取字符串类型键值,其函数原型如下所示:
int Ini_GetStringIntoBuffer (IniText handle, const char sectionName[], const char tagName[], char buffer[], size_t bufferSize);
其中handle为IniText句柄,sectionName为节的名称,tagName为键的名称,buffer 为存放读取的字符串数据内存空间,bufferSize为读取字符串数据内存空间大小。

Ini_PutInt:
Ini_PutInt用于添加或修改整形键值,其函数原型如下所示:
int Ini_PutInt (IniText handle, const char sectionName[], const char tagName[], int integerValue);
其中handle为IniText句柄,sectionName为节的名称,tagName为键的名称,integerValue为写入的键值。

Ini_PutDouble:
Ini_PutDouble用于读取双浮点型键值,其函数原型如下所示:
int Ini_PutDouble (IniText handle, const char sectionName[], const char tagName[], double doubleValue);
其中handle为IniText句柄,sectionName为节的名称,tagName为键的名称,doubleValue为写入的键值。

Ini_PutString:
Ini_PutString用于读取字符串类型键值,其函数原型如下所示:
int Ini_PutString (IniText handle, const char sectionName[], const char tagName[], const char stringValue[]);
其中handle为IniText句柄,sectionName为节的名称,tagName为键的名称,stringValue为要写入的字符串键值。

Ini_WriteToFile:
Ini_WriteToFile用于将键值写入ini文件中,其函数原型如下所示:
int Ini_WriteToFile (IniText handle, const char pathname[]);
其中handle为IniText句柄,pathname为写入的ini文件路径。

Ini_Dispose:
Ini_Dispose用于清除释放Ini_New创建的资源,其函数原型如下所示:
void Ini_Dispose (IniText handle);
其中handle为Ini_New创建的句柄。

CVI读写Ini文件实例程序

从模板新建项目,拖拽控件实现如下图布局。主要练习读写字符型、整型、浮点型、布尔型变量的方法。

image.png

新建项目中要导入NI的inifile.fp文件库,同时在程序中添加inifile.h头文件。
C:\Program Files (x86)\National Instruments\CVI2019\toolslib\toolbox\inifile.fp

image.png

全部程序代码如下,记得initext变量用后释放存储。

#include      
#include 
#include "inifile.h"
#include "CviIniFile.h"

static int panelHandle;
//名字值存储
char nameValue[256]={0};
//年龄值存储
int ageValue=10;
//是否管理员存储
BOOL adminValue=TRUE;
//ip地址存储
char IPValue[20]={0};
//端口存储
int portValue = 123;
//数据大小存储
double sizeValue = 1.2;
//获取控件中的设定值到全局变量
void GetCtrlValue()
{
    GetCtrlVal(panelHandle,PANEL_NAME,nameValue);
    GetCtrlVal(panelHandle,PANEL_AGE,&ageValue);
    GetCtrlVal(panelHandle,PANEL_ADMIN,&adminValue);
    GetCtrlVal(panelHandle,PANEL_IP,IPValue);
    GetCtrlVal(panelHandle,PANEL_PORT,&portValue);
    GetCtrlVal(panelHandle,PANEL_SIZE,&sizeValue);
}
//将全局变量的值更新到控件中
void SetCtrlValue()
{
    SetCtrlVal(panelHandle,PANEL_NAME,nameValue);
    SetCtrlVal(panelHandle,PANEL_AGE,ageValue);
    SetCtrlVal(panelHandle,PANEL_ADMIN,adminValue);
    SetCtrlVal(panelHandle,PANEL_IP,IPValue);
    SetCtrlVal(panelHandle,PANEL_PORT,portValue);
    SetCtrlVal(panelHandle,PANEL_SIZE,sizeValue);
}
int main (int argc, char *argv[])
{
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;  /* out of memory */
    if ((panelHandle = LoadPanel (0, "CviIniFile.uir", PANEL)) < 0)
        return -1;
    DisplayPanel (panelHandle);
    RunUserInterface ();
    DiscardPanel (panelHandle);
    return 0;
}

int CVICALLBACK panelCB (int panel, int event, void *callbackData,
                         int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_GOT_FOCUS:

            break;
        case EVENT_LOST_FOCUS:

            break;
        case EVENT_CLOSE:

            break;
    }
    return 0;
}

int CVICALLBACK btnExit (int panel, int control, int event,
                         void *callbackData, int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_COMMIT:
            QuitUserInterface (0);
            break;
    }
    return 0;
}
//读取ini文件
int CVICALLBACK btnReadIniFile (int panel, int control, int event,
                                void *callbackData, int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_COMMIT:
            int ret =0;
            IniText ReadIniFileHandle;
            ReadIniFileHandle = Ini_New(TRUE);
            ret = Ini_ReadFromFile(ReadIniFileHandle,"config.ini");
            if(ret==0)//读取文件成功
            {
                Ini_GetStringIntoBuffer(ReadIniFileHandle,"owner","Name",nameValue,sizeof(nameValue));
                Ini_GetInt(ReadIniFileHandle,"owner","Age",&ageValue);
                Ini_GetBoolean(ReadIniFileHandle,"owner","Admin",&adminValue);
                Ini_GetStringIntoBuffer(ReadIniFileHandle,"database","IP",IPValue,sizeof(IPValue));
                Ini_GetInt(ReadIniFileHandle,"database","Port",&portValue);
                Ini_GetDouble(ReadIniFileHandle,"database","Size",&sizeValue);
                
                SetCtrlValue();
            }
                        Ini_Dispose(ReadIniFileHandle);
            break;
    }
    return 0;
}
//写参数到ini文件中
int CVICALLBACK WriteIniFile (int panel, int control, int event,
                              void *callbackData, int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_COMMIT:
            int ret =0;
            IniText SaveIniFileHandle;
            SaveIniFileHandle = Ini_New(TRUE);
            GetCtrlValue();
            Ini_PutString(SaveIniFileHandle,"owner","Name",nameValue);
            Ini_PutInt(SaveIniFileHandle,"owner","Age",ageValue);
            Ini_PutBoolean(SaveIniFileHandle,"owner","Admin",adminValue);
            Ini_PutString(SaveIniFileHandle,"database","IP",IPValue);
            Ini_PutInt(SaveIniFileHandle,"database","Port",portValue);
            Ini_PutDouble(SaveIniFileHandle,"database","Size",sizeValue);
                
            ret = Ini_WriteToFile(SaveIniFileHandle,"config.ini");
            Ini_Dispose(SaveIniFileHandle);
                
            break;
    }
    return 0;
}

欢迎交流QQ:491114509

你可能感兴趣的:(labwindows/cvi 2019入门(3)——读写INI文件)