开发者

Strim a line in a text file (Windows)

I currently have several hundred file path in lines in a txt file I need to strim e.g.

report2011510222820.html:   <td width="60%" bgcolor="#f4f4f4" class="tablebody" valign="top">C:\Users\Administrator\Desktop\calc.exe</td>

How could I take out "report2011510222820.html: &lt;td width="60%" bgcolor="#f4f4f4" class="tablebody" valign="top"&gt;" and "&lt;/td&gt;", so I am just left with:

C:\Users\Administrator\Desktop\calc.exe

The current code I have:

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
    char s[2048];
    while (fgets(s, sizeof(s), stdin))
    {
        char *pos = str开发者_如何转开发pbrk(s, "|\r\n");
        if (pos != 0)
            fputs(pos+1, stdout);
    }
    return 0;
}


To get your posted code working for the given example, the following changes could be made.

Change the strpbrk call to check for the angle bracket instead of the vertical bar (not sure if that was just a typo in the OP code or not):

  char *pos = strpbrk(s, ">\r\n");

And then change the if (pos != 0 ) statement to the following. It truncates the end of the string at the next angle bracket.

  if (pos != 0)
     {
     char *end = strrchr( pos, '<' );
     if ( end )
        *end = '\0';
     printf("%s\n", pos + 1);
     }

The result is fairly brittle, though. But depending on the input and desired use, maybe it is okay.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜