删除文本文件中的一行 (Windows)
目前,我需要在 txt 文件中删除数百行文件路径,例如
report2011510222820.html: <td width="60%" bgcolor="#f4f4f4" class="tablebody" valign="top">C:\Users\Administrator\Desktop\calc.exe</td>
如何取出“report2011510222820.html: <td width="60%" bgcolor="#f4f4f4" class= "tablebody" valign="顶部">"和“
”,所以我只剩下:
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;
}
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: <td width="60%" bgcolor="#f4f4f4" class="tablebody" valign="top">" and "</td>
", 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 = strpbrk(s, "|\r\n");
if (pos != 0)
fputs(pos+1, stdout);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使您发布的代码适用于给定的示例,可以进行以下更改。
更改
strpbrk
调用以检查尖括号而不是竖线(不确定这是否只是操作代码中的拼写错误):然后更改
if (pos ! = 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):And then change the
if (pos != 0 )
statement to the following. It truncates the end of the string at the next angle bracket.The result is fairly brittle, though. But depending on the input and desired use, maybe it is okay.