【问题标题】:Strim a line in a text file (Windows)在文本文件中修剪一行 (Windows)
【发布时间】:2011-08-22 21:14:08
【问题描述】:

我目前在一个 txt 文件中的行中有数百个文件路径,我需要删除例如

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

我怎么能取出“report2011510222820.html: &amp;lt;td width="60%" bgcolor="#f4f4f4" class="tablebody" valign="top"&amp;gt;" and "&amp;lt;/td&amp;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;
}

【问题讨论】:

  • #include &lt;stdio.h&gt; #include &lt;string.h&gt; 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; }
  • 这是我目前的代码
  • @pavium 嗨,我想有人为我编辑过
  • 一定要在C做吗?家庭作业?

标签: c windows file text


【解决方案1】:

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

更改 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);
     }

不过,结果相当脆弱。但根据输入和所需用途,也许还可以。

【讨论】:

  • @Mark Wilkins 嗨,刚刚试过,它似乎不适用于多行,只有当文本文件中只有一行时:(
  • @Den:我只用文件中的示例行进行了尝试,它有效。您的文件是否可能包含 Unicode 字符?如果是这样,您将需要使用 fgetws 和其他适当的功能。
  • 不,它没有,很奇怪,因为即使文件路径非常相似,它仍然只输出其中一个:S
  • @Mark Wilkins 例如report2011510222820.html: &lt;td width="60%" bgcolor="#f4f4f4" class="tablebody" valign="top"&gt;C:\Users\Limited\Desktop\dmon_2.exe&lt;/td&gt; report2011510222820.html: &lt;td width="60%" bgcolor="#ffffff" class="tablebody" valign="top"&gt;C:\dmon_2.exe&lt;/td&gt;
  • 它只输出其中一行,例如C:\Users\Limited\Desktop\dmon_2.exe 而不是另一个(都在不同的行)
猜你喜欢
  • 2020-03-15
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多