VC中一些保存,读取应用程序数据的方式

经验总结

VC中一些常用的保存,读取应用程序数据的方式:

1.写入系统分配ini文件(适合少量数据的存取)
存:
  WriteProfileInt
  Call this member function to write the specified value into the specified section of the application's registry or .INI file.

  WriteProfileString
  The WriteProfileString function copies a string into the specified section of the Win.ini file. If Win.ini uses Unicode characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.
取:
  相应的get函数
 
2.写入自定义.ini文件(适合应用程序的初始化参数设置)

  WritePrivateProfileString
  The WritePrivateProfileString function copies a string into the specified section of an initialization file.

  GetPrivateProfileString
  The GetPrivateProfileString function retrieves a string from the specified section in an initialization file.

  The following sample code illustrates the preceding guidelines and is based on several assumptions:

 举例:
 There is an application named App Name.
That application uses an .ini file named AppName.ini.
There is a section in the .ini file that we want to look like this:

[Section1]
  FirstKey = It all worked out okay.
  SecondKey = By golly, it works.
  ThirdKey = Another test.

The user will not have to reboot the system in order to have future invocations of the application see the mapping of the .ini file to the registry.

#include <stdio.h>
#include <windows.h>
 
int main()
{
   CHAR   inBuf[80];
   HKEY   hKey1, hKey2;
   DWORD  dwDisposition;
   LONG   lRetCode;
   CHAR   szData[] = "USR:App Name//Section1";
 
   // Create the .ini file key.
   lRetCode = RegCreateKeyEx ( HKEY_LOCAL_MACHINE,
       "SOFTWARE//Microsoft//Windows NT//CurrentVersion//IniFileMapping//appname.ini",
       0,
       NULL,
       REG_OPTION_NON_VOLATILE,
       KEY_WRITE,
       NULL,
       &hKey1,
       &dwDisposition);
 
   if (lRetCode != ERROR_SUCCESS)
   {
      printf ("Error in creating appname.ini key/n");
      return (0) ;
   }
 
   // Set a section value
   lRetCode = RegSetValueEx ( hKey1,
                              "Section1",
                              0,
                              REG_SZ,
                              szData,
                              sizeof(szData));
 
   if (lRetCode != ERROR_SUCCESS)
   {
      printf ("Error in setting Section1 value/n");
      // Close the key
      lRetCode = RegCloseKey( hKey1 );
      if( lRetCode != ERROR_SUCCESS )
      {
         printf("Error in RegCloseKey/n");
         return (0) ;
      }
   }
 
   // Create an App Name key
   lRetCode = RegCreateKeyEx ( HKEY_CURRENT_USER,
                               "App Name",
                               0,
                               NULL,
                               REG_OPTION_NON_VOLATILE,
                               KEY_WRITE,
                               NULL,
                               &hKey2,
                               &dwDisposition);
 
   if (lRetCode != ERROR_SUCCESS)
   {
      printf ("Error in creating App Name key/n");

      // Close the key
      lRetCode = RegCloseKey( hKey2 );
      if( lRetCode != ERROR_SUCCESS )
      {
         printf("Error in RegCloseKey/n");
         return (0) ;
      }
   }
 
   // Force the system to read the mapping into shared memory
   // so that future invocations of the application will see it
   // without the user having to reboot the system
   WritePrivateProfileStringW( NULL, NULL, NULL, L"appname.ini" );
 
   // Write some added values
   WritePrivateProfileString ("Section1",
                              "FirstKey",
                              "It all worked out OK.",
                              "appname.ini");
   WritePrivateProfileString ("Section1",
                              "SecondKey",
                              "By golly, it works!",
                              "appname.ini");
   WritePrivateProfileString ("Section1",
                              "ThirdKey",
                              "Another test...",
                              "appname.ini");

   // Test
   GetPrivateProfileString ("Section1",
                            "FirstKey",
                            "Error: GPPS failed",
                            inBuf,
                            80,
                            "appname.ini");
   printf ("%s", inBuf);
 
   // Close the keys
   lRetCode = RegCloseKey( hKey1 );
   if( lRetCode != ERROR_SUCCESS )
   {
      printf("Error in RegCloseKey/n");
      return(0);
   }

   lRetCode = RegCloseKey( hKey2 );
   if( lRetCode != ERROR_SUCCESS )
   {
      printf("Error in RegCloseKey/n");
      return(0);
   }
  
   return(1);
}


3.注册表内存取(适合一些固定数据参数的读写)
存:
DWORD SetRegistry(char *szRegPort, char *szKey, char *szValue)
{
    char    ValueName[128];
    HKEY    hkPort;
    DWORD    cbData = 256;
    DWORD    retCode, dwDisp;

    if ((retCode=RegCreateKeyEx(HKEY_LOCAL_MACHINE, szRegPort, 0, NULL, REG_OPTION_NON_VOLATILE,
                                KEY_ALL_ACCESS, NULL, &hkPort, &dwDisp)) == ERROR_SUCCESS)
    {
        lstrcpy(ValueName, szKey);

        retCode = RegSetValueEx(hkPort,                    // Key handle returned from RegOpenKeyEx.
                                ValueName,                // Name of value.
                                0,                        //reserved , must be null
                                REG_SZ,                    // Type of data.
                                (PBYTE)szValue,            // Data buffer.
                                strlen(szValue));        // Size of data buffer.
        RegCloseKey(hkPort);
    }

    return retCode;
}
取:
DWORD QueryRegistry(char *szRegPort, char *szKey, char *szValue)
{
    char    ValueName[128];
    HKEY    hkPort;
    BYTE    bData[256];  // in registry the max length is banner user name
    DWORD    cbData = 256;
    DWORD    retCode;
    DWORD    dwType=REG_SZ;

    if ((retCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRegPort, 0, KEY_EXECUTE, &hkPort)) == ERROR_SUCCESS)
    {
        lstrcpy(ValueName, szKey);

        retCode = RegQueryValueEx (hkPort,                // Key handle returned from RegOpenKeyEx.
                                ValueName,                // Name of value.
                                NULL,                    //reserved , must be null
                                &dwType,                // Type of data.
                                bData,                    // Data buffer.
                                &cbData);                // Size of data buffer.

        if (retCode == ERROR_SUCCESS)
        {
            strcpy(szValue,(const char*)bData);
        }

        RegCloseKey(hkPort);
    }

    return retCode;
}


4.自定义格式的文件(适合大量数据存取,不一定有规律)
  主要是CFile,FILE的操作
   
5.数据库的存取(适合大量有组织的数据)   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值