我试图解析一个网页并从C中提取天气信息(我知道,受虐狂)。将一个字符串中的strstr搜索复制到字符串本身
在该页面中的其它东西,有这些行:
<dt>Chance of <span class='wx-firstletter'>rain</span>:</dt>
<dt>Wind:</dt>
<dt>Humidity:</dt>
<dt>UV Index:</dt>
<dt>Snowfall:</dt>
<dt>Sunrise:</dt>
<dt>Moonrise:</dt>
<dt>Moonphase:</dt>
<dt>Past 24-hr Precip:</dt>
<dt>Past 24-hr Snow:</dt>
<dt>Chance of <span class='wx-firstletter'>rain</span>:</dt>
<dt>Wind:</dt>
<dt>Humidity:</dt>
<dt>UV Index:</dt>
<dt>Snowfall:</dt>
<dt>Sunset:</dt>
<dt>Moonset:</dt>
<dt>Moonphase:</dt>
<dt>Past 24-hr Precip:</dt>
<dt>Past 24-hr Snow:</dt>
后,我已经下载了页面,它保存在一个文件中,并在用fread阵列读它,我用一个循环逐行读取数组,将其保存到临时数组(tmp)。 处理包含字符串< dt>的行的部分如下。
} else if (strstr(tmp,"<dt>")) {
strcpy(tmp,strstr(tmp,"<dt>")+4);
strcpy(strstr(tmp,"</dt>")," \0");
if (strstr(tmp,"Chance of"))
strcpy(tmp,"Chance of precipitation: ");
fwrite(tmp,1,strlen(tmp),file_tod);
} else if ....
一切都很顺利,除了月相和过去的24h雪线。
Chance of precipitation:
Wind:
Humidity:
UV Index:
Snowfall:
Sunrise:
Moonrise:
Mo>
phase:
Past 24-hr Precip:
Paw: 24-hr Snow:
Chance of precipitation:
Wind:
Humidity:
UV Index:
Snowfall:
Sunset:
Moonset:
Mo>
phase:
Past 24-hr Precip:
Paw: 24-hr Snow:
非但没有月相的:,我得到莫> \ n相:和而不是让过去的24小时,雪:,我得到爪:24小时雪:。 奇怪的是,只有这些特定的字符串正在发生。 我不能将字符串上strstr的结果复制到字符串本身吗?
strcpy(tmp,strstr(tmp,“”)+ 4);
这是犯罪行吗?我在其他代码中使用相同的方法,没有任何问题。 如果我使用一个中间变量(BUFF)来存储检索的strstr
} else if (strstr(tmp,"<dt>")) {
strcpy(buff,strstr(tmp,"<dt>")+4);
strcpy(strstr(buff,"</dt>")," \0");
if (strstr(buff,"Chance of"))
strcpy(buff,"Chance of precipitation: ");
fwrite(tmp,1,strlen(buff),file_tod);
} else if ....
一切正常的结果。
感谢您的任何答案,并很抱歉,如果它是非常明显的。
编辑:想出了这个
} else if (strstr(tmp,"<dt>")) {
memmove(tmp,strstr(tmp,"<dt>")+4,strlen(tmp)-(strlen(strstr(tmp,"<dt>")+4)));
*(strstr(tmp,":")+1)=' ';
*(strstr(tmp,":")+2)='\0';
if (strstr(tmp,"Chance of"))
strcpy(tmp,"Chance of precipitation: ");
fwrite(tmp,1,strlen(tmp),file_tod);
是否合法?
非常感谢您的快速回答。我会调查到memmove并发回。 – TeoBigusGeekus