首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么读取 value值呢

2012-06-14 
如何读取 value值呢?有个txt文件 内容如下:homepagehttp://192.168.45.104/TestAPI/homepagehttp://192.

如何读取 value值呢?
有个txt文件 内容如下:


homepage=http://192.168.45.104/TestAPI/

homepage=http://192.168.29.40/TestAPI


a=22

b=22


test-frequency=7380000


screen-width=1280

a=7
b=2



写一个方法 给地key值,取出相应的 value 若value相同 就取第一个

[解决办法]

C/C++ code
//有个kv.txt文件 内容如下://homepage=http://192.168.45.104/TestAPI/////homepage=http://192.168.29.40/TestAPI//////a=22////b=22//////test-frequency=7380000//////screen-width=1280////a=7//b=2////////写一个方法 给地key值,取出相应的 value 若value相同 就取第一个#include <stdio.h>char *getvalue(char *key) {    FILE *f;    char ln[80],k[40];    static char v[40];    v[0]=0;    f=fopen("kv.txt","r");    if (NULL==f) return v;    sprintf(k,"%.30s=%%39[^\n]",key);    while (1) {        if (NULL==fgets(ln,80,f)) break;        if (1==sscanf(ln,k,v)) break;    }    fclose(f);    return v;}void main() {    printf("getvalue(homepage)"     "==%s\n",getvalue("homepage")    );    printf("getvalue(a)"            "==%s\n",getvalue("a")           );    printf("getvalue(screen-width)" "==%s\n",getvalue("screen-width"));    printf("getvalue(haha)"         "==%s\n",getvalue("haha")        );}//getvalue(homepage)==http://192.168.45.104/TestAPI///getvalue(a)==22//getvalue(screen-width)==1280//getvalue(haha)== 

热点排行