- 写入
1 WritePrivateProfileStringA("hello money", infx[i].name, money, "1.ini");
- 按照字符串读取
1 GetPrivateProfileStringA("hello money", infx[i].name, "NULL", money, 40, "1.ini");
函数原型:
1 GetPrivateProfileStringA( 2 _In_opt_ LPCSTR lpAppName, 3 _In_opt_ LPCSTR lpKeyName, 4 _In_opt_ LPCSTR lpDefault, 5 _Out_writes_to_opt_(nSize, return +1) LPSTR lpReturnedString, 6 _In_ DWORD nSize, 7 _In_opt_ LPCSTR lpFileName 8 );
1 GetPrivateProfileIntA( 2 _In_ LPCSTR lpAppName, 3 _In_ LPCSTR lpKeyName, 4 _In_ INT nDefault, 5 _In_opt_ LPCSTR lpFileName 6 );
按照int类型读取,结果在返回值
1 int num=0; 2 num=GetPrivateProfileIntA("hello money", infx[i].name, num, "1.ini");//结果在返回值
完整代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include <Windows.h> 5 6 //创建结构体写入ini 7 struct info 8 { 9 char name[100]; 10 int money; 11 12 }; 13 14 void main() 15 { 16 //初始化 17 struct info infx[5] = { { "xiaowang", 1000000 }, { "xiaoli", 10000009 }, { "xiaosun", 9999999 }, { "xiaozhou", 8888 }, { "xiaobin", 999999999 } }; 18 //写入ini文件 19 //for (int i = 0; i < 5;i++) 20 //{ 21 // char money[40] = { 0 }; 22 // //把int类型转换为char*类型 23 // _itoa(infx[i].money, money, 10); 24 // //不指定路径,会写入系统目录 25 // WritePrivateProfileStringA("hello money", infx[i].name, money, "F:\智锋\20150526\ini文件\ini文件\1.ini"); 26 //} 27 28 for (int i = 0; i < 5;i++) 29 { 30 //根据姓名读取到字符串 31 //char money[40] = { 0 }; 32 //GetPrivateProfileStringA("hello money", infx[i].name, "NULL", money, 40, "F:\智锋\20150526\ini文件\ini文件\1.ini"); 33 //printf(" %s %s",infx[i].name, money); 34 /*GetPrivateProfileStringA( 35 _In_opt_ LPCSTR lpAppName, 36 _In_opt_ LPCSTR lpKeyName, 37 _In_opt_ LPCSTR lpDefault, 38 _Out_writes_to_opt_(nSize, return +1) LPSTR lpReturnedString, 39 _In_ DWORD nSize, 40 _In_opt_ LPCSTR lpFileName 41 );*/ 42 /* GetPrivateProfileIntA( 43 _In_ LPCSTR lpAppName, 44 _In_ LPCSTR lpKeyName, 45 _In_ INT nDefault, 46 _In_opt_ LPCSTR lpFileName 47 );*/ 48 //根据姓名读取到int类型中 49 int num=0; 50 num = GetPrivateProfileIntA("hello money", infx[i].name, num, "1.ini"); 51 printf("%d ", num); 52 } 53 system("pause"); 54 }