原创 Labwindows/cvi8.5学习日志(51) 读写 ini 文件 的简单示例

2010-8-25 16:38 5217 8 8 分类: 软件与OS

//*********************************************************
// Labwindows/cvi8.5 读写 ini 文件 的简单示例
// szlihongtao
// 2010-08-25
//*********************************************************  
#include <utility.h>
#include <userint.h>
#include <formatio.h>
#include "inifile.h"
#include "ini.h"
//******************************************************************************
static int panelHandle;
static char  g_fileName[MAX_PATHNAME_LEN];  
static IniText g_myInifile = 0;


struct   
{
 int Boolean;
 int uint;
 double db;
 char string[128];
}opt;
//****************************************************************************** 
const char sectionName0[]="sect0";
const char sectionName1[]="sect1";


const char itemName0[]="Boolean__";
const char itemName1[]="uint__";
const char itemName2[]="db__";
const char itemName3[]="string__";  
//******************************************************************************
void opt_default(void)
{
 opt.Boolean=1;
 opt.uint=5158;
 opt.db=12345.7821;
 strcpy(opt.string,"苏州xxxx公司");
}
//******************************************************************************
void init(void)
{
 int temp,status;
 int FileHandle;
 char *stringValue;
 double dtemp;


 opt_default();
 
 GetProjectDir (g_fileName); 
 strcat (g_fileName, "\\1.ini");     // 完整的文件名  
 if (FileExists(g_fileName,0)==TRUE) // 判断文件是否存在 
 {
  if (!(g_myInifile = Ini_New (0)))  /* Create a new Inifile object and read it from a file */
  {
      MessagePopup("Inifile","Error allocating memory for Inifile,无法在内存中定位文件!请联系生产厂家!");
      return;
  }


   if (Ini_ReadFromFile (g_myInifile, g_fileName))
    {
      MessagePopup("Inifile","Error reading Inifile,无法读取ini文件!请联系生产厂家!");
         return;
   }
      
  status=Ini_GetBoolean (g_myInifile, sectionName0, itemName0, &temp);
  if (status>0)
   opt.Boolean=temp;
  
  status=Ini_GetInt (g_myInifile, sectionName0, itemName1, &temp);
  if (status>0)
   opt.uint=temp;
   
  status=Ini_GetDouble (g_myInifile, sectionName0, itemName2, &dtemp);
  if (status>0)
   opt.db=dtemp;
  
  status=Ini_GetPointerToRawString (g_myInifile, sectionName0, itemName3, &stringValue);
  if (status>0)
   strcpy(opt.string,stringValue);
  
  Ini_Dispose (g_myInifile);
     g_myInifile = 0;
 }
 else
 {
  FileHandle = OpenFile ("1.ini", VAL_READ_WRITE, VAL_TRUNCATE, VAL_ASCII); // 创建一个新文件
  CloseFile(FileHandle);  // 关闭文件
 }
}
//******************************************************************************
void disp_opt(void)
{
 SetCtrlVal(panelHandle,PANEL_BINARYSWITCH,opt.Boolean);
 SetCtrlVal(panelHandle,PANEL_NUMERIC_INT,opt.uint);
 SetCtrlVal(panelHandle,PANEL_NUMERIC_DOUBLE,opt.db);
 SetCtrlVal(panelHandle,PANEL_STRING1,opt.string);
}
//******************************************************************************
int main (int argc, char *argv[])
{
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;
    if ((panelHandle = LoadPanel (0, "ini.uir", PANEL)) < 0)
        return -1;
     
 init();
 disp_opt();
 DisplayPanel (panelHandle);
 RunUserInterface (); 
    DiscardPanel (panelHandle);
    CloseCVIRTE ();
    return 0;
}
int CVICALLBACK cb_bool (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int temp;
 
 switch (event)
 {
  case EVENT_COMMIT:
  {
   if (!(g_myInifile = Ini_New (0)))  /* Create a new Inifile object and read it from a file */
   {
       MessagePopup("Inifile","Error allocating memory for Inifile,无法在内存中定位文件!请联系生产厂家!");
       return 0;
   }


    if (Ini_ReadFromFile (g_myInifile, g_fileName))
     {
       MessagePopup("Inifile","Error reading Inifile,无法读取ini文件!请联系生产厂家!");
          return 0;
    }  
 
   GetCtrlVal(panel,control,&temp);
   Ini_PutInt (g_myInifile, sectionName0, itemName0, temp);
  
      Ini_WriteToFile (g_myInifile, g_fileName);
     Ini_Dispose (g_myInifile);    
      g_myInifile = 0;   
  
   break;
  }
 }
 return 0;
}
//******************************************************************************
int CVICALLBACK cb_int (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int temp;
 
 switch (event)
 {
  case EVENT_COMMIT:
  {
   if (!(g_myInifile = Ini_New (0)))  /* Create a new Inifile object and read it from a file */
   {
       MessagePopup("Inifile","Error allocating memory for Inifile,无法在内存中定位文件!请联系生产厂家!");
       return 0;
   }


    if (Ini_ReadFromFile (g_myInifile, g_fileName))
     {
       MessagePopup("Inifile","Error reading Inifile,无法读取ini文件!请联系生产厂家!");
          return 0;
    }  
 
   GetCtrlVal(panel,control,&temp);
   Ini_PutInt (g_myInifile, sectionName0, itemName1, temp);
  
      Ini_WriteToFile (g_myInifile, g_fileName);
     Ini_Dispose (g_myInifile);    
      g_myInifile = 0;   
  
   break;
  }
 }
 return 0;
}
//******************************************************************************
int CVICALLBACK cb_double (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 double dtemp;
 
 switch (event)
 {
  case EVENT_COMMIT:
  {
   if (!(g_myInifile = Ini_New (0)))  /* Create a new Inifile object and read it from a file */
   {
       MessagePopup("Inifile","Error allocating memory for Inifile,无法在内存中定位文件!请联系生产厂家!");
       return 0;
   }


    if (Ini_ReadFromFile (g_myInifile, g_fileName))
     {
       MessagePopup("Inifile","Error reading Inifile,无法读取ini文件!请联系生产厂家!");
          return 0;
    }  
 
   GetCtrlVal(panel,control,&dtemp);
   Ini_PutDouble (g_myInifile, sectionName0, itemName2, dtemp);
   
  
      Ini_WriteToFile (g_myInifile, g_fileName);
     Ini_Dispose (g_myInifile);    
      g_myInifile = 0;   
  
   break;
  }
 }
 return 0;
}
//******************************************************************************
int CVICALLBACK cb_string (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{    
 switch (event)
 {
  case EVENT_COMMIT:
  {
   if (!(g_myInifile = Ini_New (0)))  /* Create a new Inifile object and read it from a file */
   {
       MessagePopup("Inifile","Error allocating memory for Inifile,无法在内存中定位文件!请联系生产厂家!");
       return 0;
   }


    if (Ini_ReadFromFile (g_myInifile, g_fileName))
     {
       MessagePopup("Inifile","Error reading Inifile,无法读取ini文件!请联系生产厂家!");
          return 0;
    }  
 
   GetCtrlVal(panel,control,opt.string);
   Ini_PutRawString (g_myInifile, sectionName0, itemName3, opt.string);
  
      Ini_WriteToFile (g_myInifile, g_fileName);
     Ini_Dispose (g_myInifile);    
      g_myInifile = 0;   
  
   break;
  }
 }
 return 0;
}
//******************************************************************************
int CVICALLBACK cb_add (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
  {
   if (!(g_myInifile = Ini_New (0)))  /* Create a new Inifile object and read it from a file */
   {
       MessagePopup("Inifile","Error allocating memory for Inifile,无法在内存中定位文件!请联系生产厂家!");
       return 0;
   }


    if (Ini_ReadFromFile (g_myInifile, g_fileName))
     {
       MessagePopup("Inifile","Error reading Inifile,无法读取ini文件!请联系生产厂家!");
          return 0;
    }  
 
   Ini_PutRawString (g_myInifile, sectionName1, "item0", "new0");   
  
      Ini_WriteToFile (g_myInifile, g_fileName);
     Ini_Dispose (g_myInifile);    
      g_myInifile = 0;   
  
   break;
  }
 }
 return 0;
}
//******************************************************************************
/* QuitCallback                                                             */
//******************************************************************************
int CVICALLBACK QuitCallback (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    switch (event)
 {
        case EVENT_COMMIT:
            QuitUserInterface (0);
            break;
    }
    return 0;
}
//******************************************************************************
//******************************************************************************
//******************************************************************************


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
8
关闭 站长推荐上一条 /1 下一条