我填充结构数组的代码结构的另一个数组中,并在它里面,但我会卡在环。我测试的每一个分支和循环:它的工作原理。但环不起作用。
在service_data _func
我试着分析文本,并将其添加到结构。
我把它的主要功能,并通过文本,并尝试打印存储在我在它的工作循环的每一步使用打印命令,但得到了它它不工作的值。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct service_charge {
int from;
int to;
int charge;
int slap;
char percentage[5];
};
struct service {
int id;
int provider_id;
char name[30];
int price_type;
int min_value;
int max_value;
int sort_order;
char inquiry_required[5];
struct service_charge charge_arr[10];
};
struct service serv_data[8];
char text[1000]="{\"success\": true,\"language\": \"en\",\"action\":
\"GetServiceList\",\"version\": 1,\"data\": {\"service_list\":
[{\"id\": 4806,\"provider_id\": 581,\"name\": \"Bill Payment (MG SC
AC)\",\"price_type\": 0,\"min_value\": 30,\"max_value\":
10000,\"sort_order\": 2,\"inquiry_required\":
true,\"service_charge_list\": [{\"from\": 1,\"to\": 547,\"charge\":
1,\"slap\": 1,\"percentage\": true1},{\"from\": 2,\"to\":
54875,\"charge\": 4,\"slap\": 5,\"percentage\": true1},,
{\"from\": 2,\"to\": 68945,\"charge\": 4,\"slap\":
5,\"percentage\": true2}]}";
void service_data_func (char text[]) {
int i=0;
int Wstart=0;
int Wend=0;
char name[19]= {0x20};
char name1[19]= {0x20};
int menunum=0;
int len;
len=strlen(text);
int menunum_charge=0;
while (1)//while ALL
{
if(i>=len) {
break;
}
if(text[i] == '"' && text[i+1] == 'i'&&
text[i+2] == 'd') {
while (1) { //while "id
if(text[i] == ':') {
Wstart=i+1;
Wend=0;
i++;
} else if(text[i] == ',' || text[i] == '}' ) {
Wend=i;
strncpy(name,text+Wstart,Wend-Wstart);
serv_data[menunum].id=atoi(name);
memset(name, 0, sizeof(name));
i++;
break;
} else {
i=i+1;
}
}//while id
} else if(text[i] == 's' && text[i+1] == 'e'&&
text[i+2] == 'r'&& text[i+3] == 'v'&& text[i+4] == 'i'&&
text[i+5] == 'c'&& text[i+6] == 'e'&& text[i+7] == '_'&& text[i+8]
== 'c'&& text[i+9] == 'h'&& text[i+10] == 'a'&& text[i+11] ==
'r'&& text[i+12] == 'g'&& text[i+13] == 'e'&& text[i+14] == '_'&&
text[i+15] == 'l'&& text[i+16] == 'i'&& text[i+17] == 's'&&
text[i+18] == 't') {
while (1)//while ALL
{
if(i>=len) {
break;
}
if(text[i] == 'f' && text[i+1] == 'r'&&
text[i+2] == 'o'&& text[i+3] == 'm') {
while (1) { //while from
if(text[i] == ':') {
Wstart=i+1;
Wend=0;
i++;
} else if(text[i] == ',' || text[i] == '}' ) {
Wend=i;
strncpy(name,text+Wstart,Wend-Wstart);
serv_data[menunum].charge_arr[menunum_charge].from=atoi(name);
memset(name, 0, sizeof(name));
i++;
break;
} else {
i=i+1;
}
}
} else {
i++;
}
}
} else {
i++;
}
}
}
int main()
{
service_data_func(text);
printf("%d\n",serv_data[0].charge_arr[0].from);
return 0;
}
当你到达的情况下“service_charge_list”你提取的“从”你错过添加break;
走出while (1)//while ALL
,所以你继续搜索的价值和你达到“从”下一个甚至是一个不符合同样的情况,所以你用2替换值1。
比如,你可以更换:
if(text[i] == 'f' && text[i+1] == 'r'&&
text[i+2] == 'o'&& text[i+3] == 'm') {
while (1) { //while from
...
}
} else {
i++;
}
通过(在else可被移除)
if(text[i] == 'f' && text[i+1] == 'r'&&
text[i+2] == 'o'&& text[i+3] == 'm') {
while (1) { //while from
...
}
break;
} else {
i++;
}
现在执行打印1,并对应于serv_data[0].id
估价4806
补充说明:
(1)如A码
if(text[i] == 's' && text[i+1] == 'e'&&
text[i+2] == 'r'&& text[i+3] == 'v'&& text[i+4] == 'i'&&
text[i+5] == 'c'&& text[i+6] == 'e'&& text[i+7] == '_'&& text[i+8]
== 'c'&& text[i+9] == 'h'&& text[i+10] == 'a'&& text[i+11] ==
'r'&& text[i+12] == 'g'&& text[i+13] == 'e'&& text[i+14] == '_'&&
text[i+15] == 'l'&& text[i+16] == 'i'&& text[i+17] == 's'&&
text[i+18] == 't')
...
是不可读的,难以维护和可以很容易地包含一个错误。它可以只是:
if (!strncmp(text + 1, "service_charge_list", 19))
...
当然这也是在较小的情况下,真实的。
(2)这是危险的和无用的,得到大小在:
char text[1000]="{\"success\": true,\"language\": \"en\",\"action\":\"GetServiceList\",\"version\": 1,\"data\": {\"service_list\":[{\"id\": 4806,\"provider_id\": 581,\"name\": \"Bill Payment (MG SCAC)\",\"price_type\": 0,\"min_value\": 30,\"max_value\":10000,\"sort_order\": 2,\"inquiry_required\":true,\"service_charge_list\": [{\"from\": 1,\"to\": 547,\"charge\":1,\"slap\": 1,\"percentage\": true1},{\"from\": 2,\"to\":54875,\"charge\": 4,\"slap\": 5,\"percentage\": true1},,{\"from\": 2,\"to\": 68945,\"charge\": 4,\"slap\":5,\"percentage\": true2}]}";
做就是了
char text[]="{\"success\": true,\"language\": \"en\",\"action\":\"GetServiceList\",\"version\": 1,\"data\": {\"service_list\":[{\"id\": 4806,\"provider_id\": 581,\"name\": \"Bill Payment (MG SCAC)\",\"price_type\": 0,\"min_value\": 30,\"max_value\":10000,\"sort_order\": 2,\"inquiry_required\":true,\"service_charge_list\": [{\"from\": 1,\"to\": 547,\"charge\":1,\"slap\": 1,\"percentage\": true1},{\"from\": 2,\"to\":54875,\"charge\": 4,\"slap\": 5,\"percentage\": true1},,{\"from\": 2,\"to\": 68945,\"charge\": 4,\"slap\":5,\"percentage\": true2}]}";
要么
const char * text="{\"success\": true,\"language\": \"en\",\"action\":\"GetServiceList\",\"version\": 1,\"data\": {\"service_list\":[{\"id\": 4806,\"provider_id\": 581,\"name\": \"Bill Payment (MG SCAC)\",\"price_type\": 0,\"min_value\": 30,\"max_value\":10000,\"sort_order\": 2,\"inquiry_required\":true,\"service_charge_list\": [{\"from\": 1,\"to\": 547,\"charge\":1,\"slap\": 1,\"percentage\": true1},{\"from\": 2,\"to\":54875,\"charge\": 4,\"slap\": 5,\"percentage\": true1},,{\"from\": 2,\"to\": 68945,\"charge\": 4,\"slap\":5,\"percentage\": true2}]}";
(3)在
char name[19]= {0x20};
char name1[19]= {0x20};
没有理由设置名称“”的第一个字符,而名1是未使用的,可以拆卸
(4)在这样的情况下:
if(text[i] == '"' && text[i+1] == 'i'&&
text[i+2] == 'd') {
while (1) { //while "id
if(text[i] == ':') {
...
你再次测试前3个字符,更好而之前做i += 3;
(5)你复制一个类似的代码搜索的关键字,并提取值,就不能继续为所有的情况下做的那样,它已经是太多和你管理少数情况下,使用专用的功能为
(6)基兰Biradar说,在此言你有无限循环终于生产出的访问与相关未定义行为的文本的几种情况下,如果文字是无效