栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

测试windows ini API 确定ini文件的文法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

测试windows ini API 确定ini文件的文法

标题INI 的文法分析

ini 文件格式因为比较古老,网上也找不到具体的资料,只能找到例子,为了将ini解析到哈希表中,哈希表是 map>(C++语言),研究了一下它的语法。

ini 包含的字符

[ ] = ; ‘’ “”
[ ] 是在section中使用, = 是在 赋值 中使用,";" 是在注解中使用,‘’ 在值中使用,"" 也是在值中使用.

ini结构

[section]
app1 = value1
app2 = value2

[section1]



表(1)
根据微软的API描述 ini 的构造如 表(1)

现在我们要研究一下 “section " 的格式是什么样的
先将结论写下:
在 section中前面空格和后面空格都会被修剪掉,夹在文字中的空格保留。
在其中 [ ]都可以使用
app 不以 ; 开始,不以 =或换行结束的任何串
value
支持 十进制 比如 1234
支持 八进制 比如 0o100
支持 二进制 比如 0b100
支持 十六进制 比如 0x64
支持串,取从=往后换行前的所有字符,不以‘’或”" 封闭的、以换行结尾,取其值,并将前空格尾空格剪裁掉,不以‘’ 封闭的、以换行结尾,取其值
注:十六进制不能用 0X 八进制 不能用 0O 二进制 不能用 0B 开始
中间没有 = 部分的行会被自动过滤,可以使用这种规则,加入自己的注解

C++ 例子:
#include 
#include 
void test_ini(const char* section_name)
{
    const char * INI_PATH = "E:\develop\cpp\ini序列化工具\ini序列化工具\test.ini";
    char hello[32]={};
    int num;
    int age;
    int one_23;
    int xx_xx;
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    int num6;
    int num7;
    int num8;

    char welcome[32] = {};
    char test[32] = {};
    char test1[32] = {};
    char test_key[64]={};
    num = GetPrivateProfileIntA(section_name,"num",0,INI_PATH);
    printf("num=%dn",num);
    age = GetPrivateProfileIntA(section_name,"age",0,INI_PATH);
    printf("age=%dn",age);
    one_23 = GetPrivateProfileIntA(section_name,"123",0,INI_PATH);
    printf("123=%dn",one_23);
    xx_xx = GetPrivateProfileIntA(section_name,"xx xx",0,INI_PATH);
    printf("xx_xx=%dn",xx_xx);
    num1 = GetPrivateProfileIntA(section_name,"num1",0,INI_PATH);
    printf("num1=%dn",num1);
    num2 = GetPrivateProfileIntA(section_name,"num2",0,INI_PATH);
    printf("num2=%dn",num2);
    num3 = GetPrivateProfileIntA(section_name,"num3",0,INI_PATH);
    printf("num3=%dn",num3);
    num4 = GetPrivateProfileIntA(section_name,"num4",0,INI_PATH);
    printf("num4=%dn",num4);
    num5 = GetPrivateProfileIntA(section_name,"num5",0,INI_PATH);
    printf("num5=%dn",num5);
    num6 = GetPrivateProfileIntA(section_name,"num6",0,INI_PATH);
    printf("num6=%dn",num6);
    num7 = GetPrivateProfileIntA(section_name,"num7",0,INI_PATH);
    printf("num7=%dn",num7);
    num8 = GetPrivateProfileIntA(section_name,"num8",0,INI_PATH);
    printf("num8=%dn",num8);

    GetPrivateProfileStringA(section_name,"hello","hello",hello,ARRAYSIZE(hello),INI_PATH);
    printf("hello=%sn",hello);
    GetPrivateProfileStringA(section_name,"welcome","welcome",welcome,ARRAYSIZE(welcome),INI_PATH);
    printf("welcome=%sn",welcome);
    GetPrivateProfileStringA(section_name,"test","test",test,ARRAYSIZE(test),INI_PATH);
    printf("test=%sn",test);
    GetPrivateProfileStringA(section_name,"test1","test1",test1,ARRAYSIZE(test1),INI_PATH);
    printf("test1=%sn",test1);
    GetPrivateProfileStringA(section_name,""测试key"","test_key",test_key,ARRAYSIZE(test_key),INI_PATH);
    printf(""测试key"=%sn",test_key);
    GetPrivateProfileStringA(section_name,"测试key","test_key",test_key,ARRAYSIZE(test_key),INI_PATH);
    printf("测试key=%sn",test_key);
    GetPrivateProfileStringA(section_name,"测试等号","test_key",test_key,ARRAYSIZE(test_key),INI_PATH);
    printf("测试等号=%sn",test_key);
    GetPrivateProfileStringA(section_name,";测试分号","test_key",test_key,ARRAYSIZE(test_key),INI_PATH);
    printf(";测试分号=%sn",test_key);
    GetPrivateProfileStringA(section_name,"#测试#","test_key",test_key,ARRAYSIZE(test_key),INI_PATH);
    printf("#测试#=%sn",test_key);
    GetPrivateProfileStringA(section_name,"string","string",test_key,ARRAYSIZE(test_key),INI_PATH);
    printf("string=%sn",test_key);
    GetPrivateProfileStringA(section_name,"string1","string1",test_key,ARRAYSIZE(test_key),INI_PATH);
    printf("string1=%sn",test_key);

}
int main(int argc,char** argv)
{
    test_ini("app");
    printf("1n");
    test_ini(""app"");
    printf("2n");
    test_ini(""[app]"");
    printf("3n");
    test_ini("[app]");
    printf("4n");
    test_ini("[app");
    printf("5n");
    test_ini("[app]]");
return 0;
}

例子文件:
[app]
hello="您好"
welcome = 欢迎您来 	hehe
123 = 456
age=100
num = 1 
num1 = '100'
num2 = "100"
num3 = 0x64
num4 = 0b111
num5 = 0o111
num6 = 0O111
num7 = 0x64
num8 = 0B111
xx xx = 1000
test = "hello "
test1 = 'hello 	'
"测试key"="eeee"
;hehehehehe
#ssss
sadkfjaslfjasdfk

测试key="eeee" ddd 'eeeee'
 测试等号 = = dasfasdfasf = =sss=sdfsfs = [];afasfasfasfasf
;测试分号 = adfasdfasf
#测试# = adfasdfasf
string="adfasfasfasf""
string1='"adfasfasfasf""''''''

["app"]
hello="您好"
welcome = 欢迎您来 	hehe
123 = 456
age=100
num = 1 
num1 = '100'
num2 = "100"
num3 = 0x64
num4 = 0b111
num5 = 0o111
xx xx = 1000
test = "hello "
test1 = 'hello 	'
"测试key"="eeee"
;hehehehehe
#ssss
sadkfjaslfjasdfk

测试key="eeee" ddd 'eeeee'
 测试等号 = = dasfasdfasf = =sss=sdfsfs = [];afasfasfasfasf
;测试分号 = adfasdfasf
#测试# = adfasdfasf

["[app]"]
hello="您好"
welcome = 欢迎您来 	hehe
123 = 456
age=100
num = 1 
num1 = '100'
num2 = "100"
num3 = 0x64
num4 = 0b111
num5 = 0o111
xx xx = 1000
test = "hello "
test1 = 'hello 	'
"测试key"="eeee"
;hehehehehe
#ssss
sadkfjaslfjasdfk

测试key="eeee" ddd 'eeeee'
 测试等号 = = dasfasdfasf = =sss=sdfsfs = [];afasfasfasfasf
;测试分号 = adfasdfasf
#测试# = adfasdfasf

[[app]
hello="您好"
welcome = 欢迎您来 	hehe
123 = 456
age=100
num = 1 
num1 = '100'
num2 = "100"
num3 = 0x64
num4 = 0b111
num5 = 0o111
xx xx = 1000
test = "hello "
test1 = 'hello 	'
"测试key"="eeee"
;hehehehehe
#ssss
sadkfjaslfjasdfk

测试key="eeee" ddd 'eeeee'
 测试等号 = = dasfasdfasf = =sss=sdfsfs = [];afasfasfasfasf
;测试分号 = adfasdfasf
#测试# = adfasdfasf

[[app]]]
hello="您好"
welcome = 欢迎您来 	hehe
123 = 456
age=100
num = 1 
num1 = '100'
num2 = "100"
num3 = 0x64
num4 = 0b111
num5 = 0o111
xx xx = 1000
test = "hello "
test1 = 'hello 	'
"测试key"="eeee"
;hehehehehe
#ssss
sadkfjaslfjasdfk

测试key="eeee" ddd 'eeeee'
 测试等号 = = dasfasdfasf = =sss=sdfsfs = [];afasfasfasfasf
;测试分号 = adfasdfasf
#测试# = adfasdfasf
结果(重定向到文件):
num=1
age=100
123=456
xx_xx=1000
num1=100
num2=100
num3=100
num4=7
num5=73
num6=0
num7=0
num8=0
hello=您好
welcome=欢迎您来 	hehe
test=hello 
test1=hello 	
"测试key"=eeee
测试key="eeee" ddd 'eeeee'
测试等号== dasfasdfasf = =sss=sdfsfs = [];afasfasfasfasf
;测试分号=test_key
#测试#=adfasdfasf
string=adfasfasfasf"
string1="adfasfasfasf""'''''
1
num=1
age=100
123=456
xx_xx=1000
num1=100
num2=100
num3=100
num4=7
num5=73
num6=0
num7=0
num8=0
hello=您好
welcome=欢迎您来 	hehe
test=hello 
test1=hello 	
"测试key"=eeee
测试key="eeee" ddd 'eeeee'
测试等号== dasfasdfasf = =sss=sdfsfs = [];afasfasfasfasf
;测试分号=test_key
#测试#=adfasdfasf
string=string
string1=string1
2
num=0
age=0
123=0
xx_xx=0
num1=0
num2=0
num3=0
num4=0
num5=0
num6=0
num7=0
num8=0
hello=hello
welcome=welcome
test=test
test1=test1
"测试key"=test_key
测试key=test_key
测试等号=test_key
;测试分号=test_key
#测试#=test_key
string=string
string1=string1
3
num=0
age=0
123=0
xx_xx=0
num1=0
num2=0
num3=0
num4=0
num5=0
num6=0
num7=0
num8=0
hello=hello
welcome=welcome
test=test
test1=test1
"测试key"=test_key
测试key=test_key
测试等号=test_key
;测试分号=test_key
#测试#=test_key
string=string
string1=string1
4
num=1
age=100
123=456
xx_xx=1000
num1=100
num2=100
num3=100
num4=7
num5=73
num6=0
num7=0
num8=0
hello=您好
welcome=欢迎您来 	hehe
test=hello 
test1=hello 	
"测试key"=eeee
测试key="eeee" ddd 'eeeee'
测试等号== dasfasdfasf = =sss=sdfsfs = [];afasfasfasfasf
;测试分号=test_key
#测试#=adfasdfasf
string=string
string1=string1
5
num=0
age=0
123=0
xx_xx=0
num1=0
num2=0
num3=0
num4=0
num5=0
num6=0
num7=0
num8=0
hello=hello
welcome=welcome
test=test
test1=test1
"测试key"=test_key
测试key=test_key
测试等号=test_key
;测试分号=test_key
#测试#=test_key
string=string
string1=string1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664380.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号