static int cmd_ini_op(int argc, char *argv[]) { const char usage[] = { \ " usage:\n" \ " ini read sector key,read sector " \ }; if(argc < 4) { printf(usage); return 0; } char buffer[20]; if (!strcmp(argv[1],"read")) { if (!read_profile_string(argv[2], argv[3], buffer, 20, "default_value", "test.ini")) printf("reaf failed!\r\n"); else printf("%s\r\n",buffer); } if (!strcmp(argv[1],"write")) { if (!write_profile_string(argv[2], argv[3], argv[4], "test.ini")) printf("reaf failed!\r\n"); } return 0; }
bool RegTool::WritePrivateProfileInt(char* lpAppName, char* lpKeyName, int Value, char* lpFileName) { char szValue[20] = { 0 }; sprintf(szValue, "%d", Value); write_profile_string(lpAppName, lpKeyName, szValue, lpFileName); return true; }
int test_proc(void) { const char *file ="myconfig.ini"; const char *section = "student"; const char *name_key = "name"; const char *age_key = "age"; char name[BUF_SIZE]={0}; char buf[MAX_FILE_SIZE]={0}; int age; int file_size; int i; //write name key value pair if(write_profile_string(section,name_key,"Tony",file)) { printf("write name pair to ini file fail\n"); return -1; } //write age key value pair if(write_profile_string(section,age_key,"20",file)) { printf("write age pair to ini file fail\n"); return -1; } printf("[%s]\n",section); //read string pair, test read string value if(read_profile_string(section, name_key, name, BUF_SIZE,"",file)) { printf("read ini file fail\n"); return -1; } else { printf("%s=%s\n",name_key,name); } //read age pair, test read int value. //if read fail, return default value age = read_profile_int(section,age_key,0,file); printf("%s=%d\n",age_key,age); return 0; }
/********************************************************************************* *函数名称:int param_set(char * id,char * value) *功能描述:修改ini文件中的一个参数,并保存INI文件 *输 入: section :ini文件参数对应的的section id :要修改的参数名称,不包含前面的"id_" value :参数值,字符串格式 *输 出: none *返 回 值: 0: ok save ,1:not find id *--------------------------------------------------------------------------------- * @修改人 修改时间 修改内容 * 白养民 2015-06-08 创建 *********************************************************************************/ int param_set_ex(char * section, char * id,char * value) { int i,i_sect; uint32_t u32data; uint8_t *p; for( i_sect = 0; i_sect < sizeof( tbl_ini_lookup ) / sizeof( struct _st_tbl_ini_lookup ); i_sect++ ) { if(strncmp(section,tbl_ini_lookup[i_sect].section,strlen(tbl_ini_lookup[i_sect].section)) == 0) { for( i = 0; i < tbl_ini_lookup[i_sect].param_size / sizeof( st_tbl_id_lookup ); i++ ) { if(strncmp( id ,tbl_ini_lookup[i_sect].param[i].id, strlen(tbl_ini_lookup[i_sect].param[i].id) ) == 0) { switch(tbl_ini_lookup[i_sect].param[i].type) { case TYPE_BYTE: printf("TYPE_BYTE val\n"); case TYPE_WORD: printf("TYPE_WORD val\n"); case TYPE_DWORD: printf("TYPE_DWORD val\n"); if( sscanf(value,"%d",&u32data) == 1) { if(tbl_ini_lookup[i_sect].param[i].type == TYPE_BYTE) { *tbl_ini_lookup[i_sect].param[i].val = u32data; } else if(tbl_ini_lookup[i_sect].param[i].type == TYPE_WORD) { *(uint16_t *)tbl_ini_lookup[i_sect].param[i].val = u32data; } else { *(uint32_t *)tbl_ini_lookup[i_sect].param[i].val = u32data; } } break; case TYPE_CAN: if(strlen(value) < 16) { p = tbl_ini_lookup[i_sect].param[i].val; memset(tbl_ini_lookup[i_sect].param[i].val,0,8); Ascii_To_Hex(p,value,8); //strncpy( (char*)p, (char*)value, strlen(value) ); } break; /* case TYPE_STR: if(strlen(value) < 32) { p = tbl_id_lookup[i].val; memset(tbl_id_lookup[i].val,0,32); strncpy( (char*)p, (char*)value, strlen(value) ); } break; */ default : printf("string val,i_sect=%d\n",i_sect); if(strlen(value) < tbl_ini_lookup[i_sect].param[i].type) { printf("string val_2\n"); p = tbl_ini_lookup[i_sect].param[i].val; memset(tbl_ini_lookup[i_sect].param[i].val,0,tbl_ini_lookup[i_sect].param[i].type); strncpy( (char*)p, (char*)value, strlen(value) ); printf("string_id=%s,string=%s\n",tbl_ini_lookup[i_sect].param[i].id,tbl_ini_lookup[i_sect].param[i].val); } break; } write_profile_string(tbl_ini_lookup[i_sect].section, id, value, DF_PARAM_INI_FILE); return 0; } } } } return 1; }
bool RegTool::WritePrivateProfileString(char* lpAppName, char* lpKeyName, char* Value, char* lpFileName) { write_profile_string(lpAppName, lpKeyName, Value, lpFileName); return true; }