English 中文(简体)
在字符串中复制字符串搜索到字符串本身
原标题:Copying a strstr search in a string to the string itself

我试图分析一个网页 并提取天气信息 从它与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);

合法吗?

问题回答

当源和目标字符串重叠未定义时, 像 strcpy () 这样的函数的行为 。

如果您必须在原位进行内存( 字符串) 移动, 请确定您知道字符串的长度, 并使用 < code> emmove () < () ; 当字符串重叠时, 这将保证有效 。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签