我试图分析一个网页 并提取天气信息 从它与C(受虐者,我知道)。
除其他外,该页有以下几行:
<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>
After I ve downloaded the page, saved it in a file and read it with in an array with fread, I use a loop to read the array line by line, saving it to a temporary array (tmp). The part that handles the lines containing the string < dt > is the following.
} else if (strstr(tmp,"<dt>")) {
strcpy(tmp,strstr(tmp,"<dt>")+4);
strcpy(strstr(tmp,"</dt>")," ");
if (strstr(tmp,"Chance of"))
strcpy(tmp,"Chance of precipitation: ");
fwrite(tmp,1,strlen(tmp),file_tod);
} else if ....
除了月亮和24小时的雪线之外 一切都很顺利
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:
Instead of getting Moonphase:, I get Mo> phase: and instead of getting Past 24h-Snow:, I get Paw: 24-hr Snow:. The strange thing is that is happening with only these particular strings. Can t I copy the result of strstr on a string to the string itself?
strcpy (tmp,strstr(tmp,),")+4; strcpy(tmp,strstrstr(tmp,,,")+4); strcpy(tmp,strstrstr(tmp,,,,")+4;
Is this the offending line? I use the same method all over the rest of the code without problems. If I use an intermediate variable (buff) to store the result of the strstr search
} else if (strstr(tmp,"<dt>")) {
strcpy(buff,strstr(tmp,"<dt>")+4);
strcpy(strstr(buff,"</dt>")," ");
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)= ;
if (strstr(tmp,"Chance of"))
strcpy(tmp,"Chance of precipitation: ");
fwrite(tmp,1,strlen(tmp),file_tod);
合法吗?