本帖最后由 Gin_Q 于 2020-3-31 20:57 编辑
- a.txt文件为原始文件
- The night falls gently. And you are not here. I missing you more and more and I start getting worried as
- I stare at the door just waiting for you to surprise me with your arrival at any moment.
- Sweet delusion... you are so far away right now that all I can ask for is that time moves faster...
复制代码
- b.txt 第一列为指定字符串,第二列为替换后的字符串
- is,!@#$%^&*
- good,!@#$%^&*
- are,!@#$%^&*
- the,!@#$%^&*
- ha ha,!@#$%^&*
- some,!@#$%^&*
- get,!@#$%^&*
- has,!@#$%^&*
- more,!@#$%^&*
- bad,!@#$%^&*
复制代码
- 按b.txt中的第一列把a.txt相应位置替换为b.txt第二列
复制代码
- 结果为:
- The night falls gently. And you !@#$%^&* not here. I m!@#$%^&*sing you !@#$%^&* and more and I start !@#$%^&*ting worried as
- I st!@#$%^&* at !@#$%^&* door just waiting for you to surpr!@#$%^&*e me with your arrival at any moment.
- Sweet delusion... you !@#$%^&* so far away right now that all I can ask for !@#$%^&* that time moves faster...
复制代码 代码如下:- #if 0
- By Gin
- Dev-C++ 5.11
- .c
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include <malloc.h>
-
- FILE* filepointer(const char *file,const char *mode);
- void insert_str(char* str,char* flag,size_t sl,size_t fn);
- void replace_gtr_str(char* str,char* flag,size_t sl,size_t fl);
- void replace_lss_str(char* str,char* flag,size_t subl,size_t fl);
- void replace_equ_str(char* str,char* flag,size_t fl);
- void scantext(FILE* fp, size_t* arr);
- void read_str(FILE* fp, char* sou, char* sub,size_t len);
- //字符串大小
- #define Max 1024
-
- int main(int argc,char **argv)
- {
- FILE* fpi=filepointer("a.txt","rb");
- FILE* fpo=filepointer("result.txt","wb");
- FILE* fp=filepointer("b.txt","rb");
-
- static char str[Max];
- static size_t text_s[2];//替换行数,列大小
- scantext(fp,text_s);
-
- char* soustr=(char*)calloc(text_s[0],sizeof(char)*text_s[1]);
- char* substr=(char*)calloc(text_s[1],sizeof(char)*text_s[1]);
-
- read_str(fp, soustr, substr,text_s[1]);
-
- register size_t i=0;
- register size_t len=text_s[1];
- char *p=NULL;
- while (fgets(str,Max,fpi))
- {
- for (i=0;i<text_s[0];i++)
- {
- p=str;
- p=strstr(p,soustr+i*len);
- if (p == NULL) continue;
- if (strlen(soustr+i*len) < strlen(substr+i*len)) replace_lss_str(p,substr+i*len,strlen(soustr+i*len),strlen(substr+i*len));
- else if (strlen(soustr+i*len) > strlen(substr+i*len)) replace_gtr_str(p,substr+i*len,strlen(soustr+i*len),strlen(substr+i*len));
- else if (strlen(soustr+i*len) == strlen(substr+i*len)) replace_equ_str(p,substr+i*len,strlen(substr+i*len));
- }
- fprintf(fpo,"%s",str);
- }
-
- fclose(fpi);
- fclose(fpo);
- free(soustr);
- free(substr);
- fclose(fp);
- return 0;
- }
-
- FILE* filepointer(const char *file,const char *mode)
- {
- FILE *fp;
- if (!(fp = fopen(file,mode)))
- {
- printf("open fail! [%s]:%s\n",file,strerror(errno));
- exit(EXIT_FAILURE);
- }
- return fp;
- }
- //获取文本高度,宽度以便于创建动态空间
- void scantext(FILE* fp, size_t* arr)
- {
- rewind(fp);
- char temp[Max]={"\0"};
- char *delp=",";
- char *ptr=NULL;
- register size_t y=0;
- register size_t j=0, k=0;
- while (fgets(temp,Max,fp))
- {
- y++;
- j=strlen(temp);//一行的长度
- ptr=strstr(temp,delp);
- k=strlen(ptr);//第二列长度;
- j-=k;//第一列的长度;
- arr[1] = arr[1] < (j > k ? j:k) ? (j > k ? j:k):arr[1];//记录最大值
- }
- arr[0] = y,arr[1]+=1;
- }
- void read_str(FILE* fp, char* sou, char* sub,size_t len)
- {
- rewind(fp);
- char temp[Max]={"\0"};
- char *p=NULL;
- register size_t i=0;
- register size_t j=0;
- register size_t k=0;
-
- while (fgets(temp,Max,fp))
- {
- j=strlen(temp);//总
- p=strstr(temp,",");
- k=strlen(p);//第2列
- memmove(sou+i*len,temp,j-k);
- memmove(sub+i*len,p+1,k);
- for (k-=2;;k--)
- if (*(sub+i*len+k) == 0xA || *(sub+i*len+k) == 0xD)
- *(sub+i*len+k) = '\0';//去掉换行和回车
- else
- break;
- i++;
- }
- }
- /*
- 在字符串开始插入指定字符串
- str 原字符串的起点
- flag 指定字符串
- sl 原字符串长度
- fl 指定字符串长度
- */
- void insert_str(char* str,char* flag,size_t sl,size_t fl)
- {
- memmove(str+fl,str,sl+1);//+1存放结束符'\0'
- memmove(str,flag,fl);
- }
- /*
- 被替换字符串长度大于替换字符字符串长度
- explanation:
- str=abcd,flag=abc
-
- str 原字符串的起点
- flag 替换的字符串
- sl 被替换字符长度
- fl 替换字符串长度
- */
- void replace_gtr_str(char* str,char* flag,size_t sl,size_t fl)
- {
- memmove(str+fl,str+sl,sl+1);
- memmove(str,flag,fl);
- }
- /*
- 被替换字符长度小于替换字符串长度
- 删除被替换字符串
- explanation:
- str=abc,flag=abcd
-
- str 原字符串的起点
- flag 替换的字符串
- subl 被替换字符长度
- fl 替换字符串长度
- */
- void replace_lss_str(char* str,char* flag,size_t subl,size_t fl)
- {
- register size_t temp=strlen(str)-subl;//剩余字符串长度
- memmove(str+fl,str+subl,temp+1);
- memmove(str,flag,fl);
- }
- /*
- 被替换字符串与指定字符串相等
- explanation:
- str=abc,flag=abc
-
- str 原字符串的起点
- flag 替换的字符串
- fl 替换字符长度
-
- */
- void replace_equ_str(char* str,char* flag,size_t fl)
- {
- memmove(str,flag,fl);
- }
复制代码
|