求助~想彻底搞定文件里任意字符串替换问题~~(用C)

DewLoVeR 2004-07-27 08:53:24
问题来历:
保存了不少csdn的xml格式的帖子,但是它们xsl文件的链接非放在根目录~不爽~我想把链接由/expert/Xsl/2.xsl 改为 ./xsl/csdn.xsl ;
还有,下的不少htm文件里面嵌入javascript代码,一打开就加载广告,我也想把它们<script和/script>之间的代码都删除掉~~
嘿嘿~那么多文件不能手工改吧~
可是我文件操作尤其不熟~搞不定这个字符串替换的问题~~
搜索了一下发现类似的问题还不少~可是没有一个说清楚的,
各位大侠都来说一下下~最好要有代码说明呀~
要考虑多种情况,比如要替换的字符串长度比源字符串长~短~或者为空~~
...全文
247 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
DewLoVeR 2004-07-27
  • 打赏
  • 举报
回复
哈~~原来是酱紫呀~~

非常感谢~~哈哈~~

稍等~晚上就揭帖~~ ^_*
geesun 2004-07-27
  • 打赏
  • 举报
回复
第一个问题:
#include<string.h>
#include<stdio.h>
int main(){
FILE *fp,*fp2;
char str[1000];
char *result = NULL;
char *p;
int len;
char des[1000];
fp = fopen("/home/stel/qixiang/csdn/1.c","r");
if(fp ==NULL)
{
printf("Can not open the file\n");
exit(0);
}
fp2 = fopen("/tmp/tem.txt","w");

while(fgets(str,sizeof(str),fp))
{
result = strstr(str,"/expert/Xsl/2.xsl");

strncpy(des,str,result- str); //process previous
p = strtok( result, "/");
len = strlen(p) + 1;
sprintf(des,"%s%s%s",des,"./",p+len);

printf("%s\n",des);
fprintf(fp2,"%s\n",des);
}
fclose(fp);
fclose(fp2);
system("cp /tmp/tem.txt /home/stel/qixiang/csdn/1.c");


}
这个是假设你一行只有一个要替换的,如果假设不对,可以加个循环实现!
至于很多文件,可以考虑文件名从文件读,然后再用循环打开文件!
peter9606 2004-07-27
  • 打赏
  • 举报
回复
用这个函数你可以把函数指针定位到你要替换的字符串处,然后删掉它,再插入你要替换的字符串
不是就可以了么?
plusKid 2004-07-27
  • 打赏
  • 举报
回复
这个吧:
#change.pl
open(FILE_NAME_LIST,"files.txt");
while($name_in = <FILE_NAME_LIST>)
{
open(FILE,$name_in);
@line = <FILE>;
$i = 0;
while($i < @line)
{
$line[$i]=~s/\/expert\/Xsl\/2\.xsl/\.\/xsl\/csdn.xsl/g;
$i++;
}
close(FILE);
open(FILE,">$name_in");
print FILE @line;
close(FILE);
}
用perl写的,下载个Active Perl就能运行了,可以解决第一个替换,第二个替换的原理差不多,perl入门很简单的,对平时的一些文本处理很有帮助,只要在files.txt里面提供要转转换的文件列表就可以了。如果硬要用C ,也可以,作为练习嘛。
peter9606 2004-07-27
  • 打赏
  • 举报
回复
楼主 你可能是不清楚strtok()的意思:
strtok
Syntax: #include <string.h>
char *strtok( char *str1, const char *str2 );



The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.
DewLoVeR 2004-07-27
  • 打赏
  • 举报
回复
55555555 ,是我没说清楚吧。。
我不是要单独替换他们啊。。这些要替换的字符串都在很多个文件里面啊
唉~这样说吧,
一个目录下面有n多(10000个~!)文件(比如25365.xml 2563562.xml 4512563.xml等等等等,
每个文件里面都包含了"/expert/Xsl/2.xsl" 这个字符串,
我想把他们统统替换为 "./xsl/csdn.xsl"——我不可能手工一个一个改的哦 -_-!!
然后,我把他们列出来,读入一个,查找字符串 "/expert/Xsl/2.xsl" 替换为 "./xsl/csdn.xsl",然后读入下一个,循环处理
现在对查找替换这个地方搞不定~~
呵呵,谢谢楼上两兄弟~~还请各位继续~~
geesun 2004-07-27
  • 打赏
  • 举报
回复
第二个问题:
#include<string.h>
#include<stdio.h>
int main(){
char str[] = "previous text <script src=http://www.abcd.com/cdef.htm> function etc </script> next text";

char des[100];
char *result = NULL;
int len;

result = strstr(str,"<script");
printf("%s\n",result);
strncpy(des,str,result - str);
result = strstr(str,"script>");
printf("%s\n",result);
strcat(des,result+7);

printf("%s\n",des);
}

加个循环应该可以实现搂住的功能了!
geesun 2004-07-27
  • 打赏
  • 举报
回复
第一个问题可以这样解决:
#include<string.h>
#include<stdio.h>
int main(){
char str[] = "/expert/Xsl/2.xsl";

char des[100];
char *result = NULL;
int len;
result = strtok( str, "/" );

len = strlen(result) + 1;
sprintf(des,"%s%s","./",result+len);
printf("%s\n",des);
}
peter9606 2004-07-27
  • 打赏
  • 举报
回复
楼住
我给你的是检索方法 你检索到后 删掉中间的就可以了呀
DewLoVeR 2004-07-27
  • 打赏
  • 举报
回复
还是用C/C++吧~~就当是学习呗~~~ *_^
DewLoVeR 2004-07-27
  • 打赏
  • 举报
回复
比如~查找字符串 "/expert/Xsl/2.xsl" 替换为 "./xsl/csdn.xsl"

或者

查找字符串 "<script src=http://www.abcd.com/cdef.htm /script>" 替换为 ""

plusKid 2004-07-27
  • 打赏
  • 举报
回复
这种问题用文本编辑器自带的文字替换功能方便得多,如果非要自己编写程序的话,建议用perl之类的对正则表达式支持很好的解释型语言来完成,用c,太麻烦了。
DewLoVeR 2004-07-27
  • 打赏
  • 举报
回复
呵呵,其实这是一个问题啊~~
读入文件~扫描文件~查找字符串~替换~读下一个文件…………
不过你写的什么~我没看明白~ -_-!!
再仔细瞧瞧~~~
peter9606 2004-07-27
  • 打赏
  • 举报
回复
第二个问题好办 第一个问题不清楚你的意思
#include<string.h>
#include<stdio.h>
void main(){
char str[] = "now # is the time for all * good men to come to the & aid of their country";
char* delims[] = {"#","*","&","\0"};
char *result = NULL;
int i = 0 ;
result = strtok( str, delims[i++] );
printf( "result is \"%s\"\n", result );
while( i < 4 && result != NULL ) {

result = strtok( NULL, delims[i++]);
printf( "result is \"%s\"\n", result );

}

}

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧