繁体   English   中英

在文本文件中粗线(Windows)

[英]Strim a line in a text file (Windows)

我目前在txt文件中需要几百行文件路径,例如

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

我如何取出“ report2011510222820.html: &lt;td width="60%" bgcolor="#f4f4f4" class="tablebody" valign="top"&gt;" and "&lt;/td&gt; report2011510222820.html: &lt;td width="60%" bgcolor="#f4f4f4" class="tablebody" valign="top"&gt;" and "&lt;/td&gt; ”,所以我只剩下:

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

我目前的代码:

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

为了使发布的代码适用于给定的示例,可以进行以下更改。

更改strpbrk调用以检查尖括号而不是竖线(不确定这是否只是OP代码中的错字):

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

然后将if (pos != 0 )语句更改为以下内容。 它会在下一个尖括号中截断字符串的结尾。

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

但是,结果非常脆弱。 但是取决于输入和所需的用途,也许还可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM