Reputation: 31
I'm trying to parse a web page and extract weather info from it with C (masochist, I know).
Among other things in that page, there are these lines:
<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>")," \0");
if (strstr(tmp,"Chance of"))
strcpy(tmp,"Chance of precipitation: ");
fwrite(tmp,1,strlen(tmp),file_tod);
} else if ....
Everything goes well, apart from the Moonphase and Past 24h snow lines.
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>\nphase: 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);
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>")," \0");
if (strstr(buff,"Chance of"))
strcpy(buff,"Chance of precipitation: ");
fwrite(tmp,1,strlen(buff),file_tod);
} else if ....
everything's ok.
Thanks for any answer and sorry if it is very obvious.
EDIT: Came up with this
} 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);
Is it legit?
Upvotes: 1
Views: 1369
Reputation: 754410
The behaviour of functions like strcpy()
when the source and target strings overlap is undefined.
If you must do the memory (string) move in situ, make sure you know the length of the string and use memmove()
; that is guaranteed to work when the strings overlap.
Upvotes: 2