获取文件某个字段对应的整型值 key:value
long long get_file_key_integer_value(const char * path, const char *key)
{
FILE *fp;
char buf[512];
char *p_dst = NULL;
char *p_rx = NULL;
long long result = 0;
if(NULL == path || NULL == key)
{
printf("file path is null or the key is null\n");
return -1;
}
fp = fopen(path,"r");
if(!fp)
{
perror("fopen\n");
return -1;
}
while(fgets(buf,sizeof(buf),fp))
{
p_dst = strstr(buf,key);
if(p_dst)
{
p_rx = strstr(p_dst,":");
if(p_rx == NULL)
{
fclose(fp);
return -1;
}
result = strtoll(p_rx+1,NULL,10);
//printf("[get_file_key_integer_value] %s = %lld\n",key,result);
fclose(fp);
return result;
}
}
fclose(fp);
return -1;
}