• Windows专门针对INI文件的API


    主要有:ReadPrivateProfileString WritePrivateProfileString ReadPrivateProfileInit WritePrivateProfileInt

    需要注意的一点是:GET方法可能需要使用者提供一个如果获取失败用来代替的值。

    测试代码:

    void main()
    {
        const int LEN = 100;
        WCHAR buffer[LEN] = {0};
        DWORD lSize = GetPrivateProfileString(L"NETWORK", L"Server", L"NotExist!", buffer, LEN, L".\Test.ini");//strange: "Test.ini" fail, but ".\Test.ini" suc
        
        WritePrivateProfileString(L"Tup", L"Sex", L"man", L".\Test.ini");
    
    
        wcout<<L"buffer:  "<<buffer<<endl;
        cout<<"return size:  "<<lSize<<endl;
    
        system("pause");
    }
    // Demo4Ini.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <windows.h>
    #include <fstream>
    #include <iostream>
    // #include <io.h>
    using namespace std;
    
    #define CFG_INI_FILENAME L"./test.ini" //晕! 如果只指定"test.ini" 会在C:Windows下创建
    #define AREA_NETWORK     L"NETWORK"
    #define KEY_SERVER       L"Server"
    #define VAL_DEF_SERVER   L"127.0.0.1"
    #define AREA_PEOPLE      L"PEOPLE"
    #define KEY_NAME         L"Name"
    #define VAL_DEF_NAME     L"Alex Hong"
    #define KEY_AGE          L"Age"
    #define VAL_DEF_AGE      L"24"
    #define VAL_DEF_NOTEXIST L"NotExist!"
    
    void EmptyIni();
    void ReadIni();
    void WriteIni();
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        EmptyIni();
        ReadIni();
        WriteIni();
        ReadIni();
    
    
    
        cout<<"OVER."<<endl;
        getchar();
    
        return 0;
    }
    
    void EmptyIni()
    {
        //一般执行步骤:删除文件,创建一个空的同名文件
        //如果删除失败,那么文件肯定被占用,此时写了也可能白写(被其他覆盖)这种情况不应该出现,所以不考虑
    
        cout<<"==============EmptyIni Module============"<<endl;
        if((_waccess(CFG_INI_FILENAME, 0) ==0 ) && !::DeleteFileW(CFG_INI_FILENAME)) //存在且删除失败
            return;
    
        wfstream fInOut(CFG_INI_FILENAME, ios::out);
        if(!fInOut.fail())
        {
            cout<<".ini file exists."<<endl;
            cout<<"clear over."<<endl;
        }
        else
        {
            cout<<".ini file doesn't exist."<<endl;
        }
    
        fInOut.close();
        fInOut.clear(); //clear all good or bad bits
    }
    
    void ReadIni()
    {
        cout<<"==================ReadIni Module================="<<endl;
        const int LEN = 100;
        WCHAR buffer[LEN] = {0};
        DWORD lSize = GetPrivateProfileStringW(AREA_NETWORK, KEY_SERVER, VAL_DEF_NOTEXIST, buffer, LEN, CFG_INI_FILENAME);
        wcout<<L"Server: "<<buffer<<"  Size: "<<lSize<<endl;
        
        lSize = GetPrivateProfileStringW(AREA_PEOPLE, KEY_NAME, VAL_DEF_NOTEXIST, buffer, LEN, CFG_INI_FILENAME);
        wcout<<L"Name: "<<buffer<<"  Size: "<<lSize<<endl;
    
        lSize = GetPrivateProfileIntW(AREA_PEOPLE, KEY_AGE, -1, CFG_INI_FILENAME);
        wcout<<L"Age:  "<<lSize<<endl;
    }
    
    void WriteIni()
    {
        cout<<"==================WriteIni Module================="<<endl;
        WritePrivateProfileStringW(AREA_NETWORK, KEY_SERVER, VAL_DEF_SERVER, CFG_INI_FILENAME);
        WritePrivateProfileStringW(AREA_PEOPLE, KEY_NAME, VAL_DEF_NAME, CFG_INI_FILENAME);
        WritePrivateProfileStringW(AREA_PEOPLE, KEY_AGE, VAL_DEF_AGE, CFG_INI_FILENAME); //int也按string写,只不过读的时候转化下
    }
  • 相关阅读:
    May LeetCoding Challenge22 之 比较器comparator、map按Value排成逆序、桶排序
    May LeetCoding Challenge21 之 动态规划的min使用
    May LeetCoding Challenge20 之 二叉树中序遍历
    May LeetCoding Challenge19 之 单调栈2.0
    May LeetCoding Challenge18 之 滑动窗口2.0
    May LeetCoding Challenge17 之 滑动窗口
    May LeetCoding Challenge16 之 链表重组
    APT常用命令
    DDCTF-misc-流量分析
    Wireshark学习笔记
  • 原文地址:https://www.cnblogs.com/tupx/p/3480898.html
Copyright © 2020-2023  润新知