c语言删除一个字符指令,【C语言】实现一个基于命令行的文本编辑器
选择本项目作业的同学,必须本人独立完成。
功能:
1、 打开文件
2、 保存文件
3、 能够对文件进行查找
4、 能够对文件进行替换
5、 能够对文件进行删除
要求:
1、 不能使用Cstring.h,string等类,只能使用char*来处理字符串
2、 能够动态的申请和分配内存
上交的内容:
1、.cpp、.h、.exe文件
2、文件命名规则:学号.cpp 学号.h 学号.exe
程序具体要求:
1、 在命令行下运行 学号.exe出现界面如下
2、 按输入参数
a) -o filename 打开文件
b) –h 帮助信息,提示各个参数的格式和含义
c) –s filename 保存文件到filename指定的路径和文件名
d) –f stringtofind 查找stringtofind字符串,并将查到的字符串的个数在屏幕上显示,并将个数写到文件 学号_result.txt中,路径和最后-s中的filename同一个路径
e) –r sourcestring targetstring 替换字符串,将文件中的sourcestring替换成targetstring,将替换多少处显示在屏幕上,并将替换的个数和替换后的文本都写道 学号_result.txt中,路径要求同上
f) –d stringtodelete 删除字符串,将文件中的stringtodelete删除,将删除多少处显示在屏幕上,并将删除的个数和删除后的文本都写到 学号_result.txt中,路径要求同上
g) –q 退出程序
3、 当前文件的问题:
sample:
打开了c:\aaa.txt
在-d删除之后,再执行-r时是要在原来的aaa.txt上进行-r操作,即不受-d的影响
一旦执行了-s
当前的文件就变成了-s保存的结果
如:打开c:\aaa.txt
在-d删除之后,执行了-s
再执行-r时就是要在删除之后的文本上进行-r操作了
4、 学号_result.txt
这个文件每次写都是在文件的最后追加,不要覆盖原来的内容
要求所有的次数独占一行,写法为:-f10 -r29 -d3
所有的文本写完后空两行
sample:
#include
#include
#include
#include
typedef struct text
{
char *h;
int sn,wn,nn,len;
}Text,*ttext;
char *ctmp;
int help();
int save();
int findstr();
int rep();
int chLen(char *);
int chCo(char *, char *, int );
int del();
void resultbulid(char ,int ,int );
char filename[20]={0};
char *result="1253007_result.txt";
char spos[200];
void buildspos(char *);
long strl(char *);
void strcpy(char *,char* );
void strcat(char *,char *);
int main(int argc ,char *argv[])
{
ctmp=(char*)calloc(2000,sizeof(ctmp));
char a;
char s[100];
int i=1;
FILE* fp;
while(i)
{
scanf("%s",s);
if(!strcmp("-o",s))
scanf("%s",filename);
else
if((fp=fopen(filename,"r"))!=NULL)
{ if(!strcmp("-h",s))
help();
else if(!strcmp("-s",s))
save();
else if(!strcmp("-f",s))
findstr();
else if(!strcmp("-r",s))
rep();
else if(!strcmp("-d",s))
del();
else if(!strcmp("-q",s))
break;
else
printf("error\n");
}
else
if(!strcmp("-h",s))
help();
else if(!strcmp("-q",s))
break;
else
printf("cannot open the file");
}
fclose(fp);
remove("temp.p");
return 0;
}
int help()
{
printf("-o filename 打开文件\n");
printf("-h 帮助信息\n");
printf("-s filename 保存文件到指定路径和文件名\n");
printf("-f stringtofind 查找stringtofind字符串,并将个数保存至文件\n");
printf("-r sourcestring targetstring 替换字符,将sourcestring替换成targetstring,并保存替换的个数\n");
printf("-d stringtodelete 删除stringtodelete字符串,并保存删除的个数\n");
printf("-q 退出程序\n");
}
void strcat(char *s,char *p)
{
int i=strl(s),j;
for (j=0;j<=strl(p);j++)
{
s[i+j]=p[j];
}
}
void buildspos(char *s)
{
int i,j;
for (i=0;i<200;i++)spos[i]=0;
for (i=strl(s)-1;i>=0;i--) if (s[i]==0x5c) break;
for (j=0;j<=i;j++)
spos[j]=s[j];
}
long strl(char *s)
{
long i=0;
for (;s[i]!='\0';i++);
return i;
}
void strcpy(char *s,char* p)
{
int i;
for (i=0;i
s[i]=p[i];
}
int save()
{
FILE *fp,*tp;
char savename[200];
scanf("%s",savename);
fp=fopen(savename,"w");
tp=fopen("temp.p","r");
while (fgets(ctmp,2000,tp)!=NULL)
{
fputs(ctmp,fp);
}
fclose(tp);
fclose(fp);
buildspos(savename);
strcat(spos,result);
tp=fopen("result.p","r");
fp=fopen(spos,"w");
while (fgets(ctmp,2000,tp)!=NULL)
{
fputs(ctmp,fp);
}
strcpy(filename,savename);
fclose(tp);
fclose(fp);
free(tp);
free(fp);
remove("result.p");
}
long pos[200],totp;
long strcp(char *tt,char *pp)
{
totp=0;
int i=0,j=0;
while (i
{
if (tt[i]==pp[j])
{
i++;
j++;
if (j==strl(pp))
{
totp++;
pos[totp]=i-j;
i++;j=0;
}
}
else {
i=i-j+1;j=0;}
}
return totp;
}
int findstr()
{
FILE *p;
p=fopen(filename,"r");
char c[100];
int i=0,j=0,ct=0,k;
scanf("%s",c);
while (fgets(ctmp,2000,p)!=NULL)
{
ctmp[strl(ctmp)-1]='\0';
ct+=strcp(ctmp,c);
}
fclose(p);
free(p);
printf("%d\n",ct);
resultbulid('f',ct,0);
}
int rep()
{
char fin[300],source[20],target[20];
int i,j,k,m,ct=0;
scanf("%s %s",source,target);
FILE *out,*p;
out=fopen("temp.p","w");
p=fopen(filename,"r");
while (fgets(ctmp,2000,p)!=NULL)
{
ctmp[strl(ctmp)-1]='\0';
ct+=strcp(ctmp,source);
k=1;
for (i=0;i
{
if (i==pos[k])
{
fprintf(out,"%s",target);
k++;
i=i+strl(source);
}
else fprintf(out,"%c",ctmp[i]);
}
fprintf(out,"\n");
}
fclose(out);
fclose(p);
printf("%d\n",ct);
free(out);
free(p);
resultbulid('r',ct,1);
}
int chLen(char *s)
{
int i = 0;
do
{
i++;
}while(s[i]!='\0');
return i;
}
int chCo(char *s, char *d, int len)
{
int i;
for(i=0; i
{
if(s[i] != d[i])
{
return 0;
}
}
return 1;
}
int del()
{
char fin[300],source[20],target[20];
int i,j,k,m,ct=0;
scanf("%s",source);
FILE *out,*p;
out=fopen("temp.p","w");
p=fopen(filename,"r");
while (fgets(ctmp,2000,p)!=NULL)
{
ctmp[strl(ctmp)-1]='\0';
ct+=strcp(ctmp,source);
k=1;
for (i=0;i
{
if (i==pos[k])
{
k++;
i=i+strl(source);
}
else fprintf(out,"%c",ctmp[i]);
}
fprintf(out,"\n");
}
fclose(out);
fclose(p);
printf("%d\n",ct);
free(out);
free(p);
resultbulid('d',ct,1);
}
void resultbulid(char p,int tot,int pri)
{
FILE *fp=fopen("result.p","a");
fprintf(fp,"-%c %d\n",p,tot);
if (pri)
{
FILE *tp=fopen("temp.p","r");
while (fgets(ctmp,2000,tp)!=NULL)
fputs(ctmp,fp);
fclose(tp);
free(tp);
}
fprintf(fp,"\n");
fclose(fp);
free(fp);
}
#include
#include
#define LEN sizeof(struct node)
struct node
{
char data;
struct node *next;
}*head1, *p1, *p2,*head2;
char *temp[100],sources[100],target[100]; //这个指针数组用来存那些配上对的链表的地址
char result[100]={0};
char **point;
void help();
void open(char *);
void Save(char *);
int search(struct node *&head,char *stringtofind);
int replace(struct node *head,node *&head1,char *sources,char *target);
int delet(struct node *head,node *&head2,char *sources);
void buildhead(struct node *head,char *);
void ArrayToList(char *,struct node *);
void print(node *,char *,char *);
void Listdel(node *&);
void initlization();
long strl(char *s);
void resultpath(char *);
int m;
int n;
int h;
int sourcestringlength;char *a[10],b[6]={'\r','\n','\r','\n','\r','\n'};
char now[255],filename[100]={0},savename[100]={0};
struct node *pos[200000][2];
FILE *fp;
int main()
{
initlization();
FILE *fp;
int i=0;
char ch;
int ILength=0;
while (1)
{
char a[7][35]= {"-o","-h","-s","-f","-r","-d","-q"};
printf("Please enter your choice:\n");
char order[5];
scanf("%s",order);
if(order[1]==a[0][1])
{
scanf("%s",filename);
open(filename);
print(head1,"CON","w");
continue;
}
if(order[1]==a[1][1])
{
help();continue;
}
if(order[1]==a[2][1])
{
scanf("%s",filename);
Save(filename);
continue;
}
if(order[1]==a[3][1])
{
char *stringtofind;FILE *cp;
stringtofind=(char *)malloc(100*sizeof(char));
scanf("%s",stringtofind);
m= search(head1,stringtofind);
printf("%d\n",m);
freopen("result.t","a",stdout);
printf("-f%d\n\n",m);
freopen("CON","w",stdout);
continue;
}
if(order[1]==a[4][1])
{
scanf("%s %s",sources,target);
n= replace( head1, head2,sources,target);
printf("%d\n",n);
print(head2,"CON","w");
freopen("result.t","a",stdout);
printf("-r%d\n",n);
print(head2,"result.t","a");
freopen("result.t","a",stdout);
printf("\n");
freopen("CON","w",stdout);
continue;
}
if(order[1]==a[5][1])
{
scanf("%s",sources);
n=delet(head1,head2,sources);
print(head2,"CON","w");
printf("%d\n",n);
freopen("result.t","a",stdout);
printf("-r%d\n",n);
print(head2,"result.t","a");
freopen("result.t","a",stdout);
printf("\n");
freopen("CON","w",stdout);
continue;
}
if (order[1]==a[6][1])
return 0;
}
fclose(fp);
putchar(10);
return 0;
}
void buildhead(struct node *head,char *filename)
{
freopen(filename,"r",stdin);
int k;
char c;
struct node *p,*q;
q=head;
head->data='\0';
c=getchar();
while (c!=EOF)
{
q->data=c;
k++;
p=(struct node*)malloc(sizeof(struct node));
q->next=p;
p->next=NULL;
q=p;
c=getchar();
}
freopen("CON","r",stdin);
}
void open(char *filename)
{
Listdel(head1);
if((fp=fopen(filename,"r"))==NULL)
printf("cannot open this file\n");
buildhead(head1,filename);
buildhead(head2,filename);
freopen("result.t","w",stdout);
freopen("CON","w",stdout);
return;
}
void Save(char *savename)
{
node *p=head2;
freopen(savename,"w",stdout);
while (p->next!=NULL)
{
printf("%c",p->data);
p=p->next;
}
resultpath(savename);
open("result.t");
print(head1,result,"w");
freopen("CON","w",stdout);
open(savename);
}
void print(node *head,char *poss,char *arg)
{
node *p=head;
freopen(poss,arg,stdout);
while (p->next!=NULL)
{
printf("%c",p->data);
p=p->next;
}
}
void help()
{
struct Choices
{
char name[30*2];
char form[20*2];
char meaning[60*2];
};
int i;
Choices cho[7]=
{
{"-o filename","字符串","打开文件"},
{"-h","字符","帮助信息"},
{"-s filename","字符串", "保存文件"},
{"-f stringtofind","字符串","查找字符串"},
{"-r sourcestring targetstring","字符串","替换字符串"},
{"-d stringtodelete","字符串","删除字符串"},
{"-q","字符","退出"}
};
for (i=0; i<7; i++)
printf("%s %s %s\n",cho[i].name,cho[i].form,cho[i].meaning);
return;
}
int search(struct node *&head1,char *stringtofind) //k为查找到的个数 0:not found
{
int i,j,k;
k=0;
int length=0;
while(stringtofind[length]!='\0')
length++;
struct node *p1,*p2;
p1=p2=(node *)malloc(sizeof(LEN));
if(head1==NULL)
{
printf("\nlist null!\n");
return 0;
}
p1=head1;
int judge=0;
p2=p1;
j=0;
while(p1->next!=NULL)
{
if (p1->data==stringtofind[j])
{
p1=p1->next;
j++;
if (j==length)
{
k++;
pos[k][0]=p2;
pos[k][1]=p1;
p1=p2->next;
p2=p1;
j=0;
}
}
else
{
p1=p2->next;
p2=p1;
j=0;
}
}
return k;
}
void ArrayToList(char *s,struct node *head)
{
int length=0;
node *p,*q;
head->next=NULL;
p=head;
head->data='\0';
while (s[length]!='\0')
{
p->data=s[length];
q=(node *)malloc(sizeof(node));
q->next=NULL;
q->data='\0';
p->next=q;
p=q;
length++;
}
}
int replace(struct node *head,struct node *&head1,char *sources,char *target)
{
Listdel(head2);
int i=0;
sourcestringlength=0;
while(sources[i]!='\0')
{
i++;
sourcestringlength++;
}
int m=search(head,sources);
node *temp;
temp=(node *)malloc(sizeof(node));
ArrayToList(target,temp);
node *start,*temp1,*headd,*qq,*pp;
headd=(node*)malloc(sizeof(node));
start=head;
headd->next=NULL;
qq=headd;
i=1;
while (start->next!=NULL)
{
if (start==pos[i][0])
{
start=pos[i][1];
temp1=temp;
i++;
while (temp1->next!=NULL)
{
qq->data=temp1->data;
pp=(node *)malloc(sizeof(node));
pp->next=NULL;
qq->next=pp;
pp->data=0;
qq=pp;
temp1=temp1->next;
}
}
else
{
qq->data=start->data;
pp=(node *)malloc(sizeof(node));
pp->next=NULL;
qq->next=pp;
pp->data=0;
qq=pp;
start=start->next;
}
}
head1=headd;
return m;
}
int delet(struct node *head,node *&head2,char *scources)
{
int i;
m=search(head,sources);
replace(head,head2,sources,"");
return m;
}
void Listdel(node *&head)
{
node *p=head,*q;
while (p->next!=NULL)
{
q=p->next;
free(p);
p=q;
}
head=(node*)malloc(sizeof(node));
head->data='\0';
head->next=NULL;
}
void initlization(){
head1=(node*)malloc(sizeof(node));
head2=(node*)malloc(sizeof(node));
head1->data=head2->data='\0';
head1->next=NULL;head2->next=NULL;}
void resultpath(char *savename)
{
int i,j,length=strl(savename);char a[20]="1253012_result.txt";
for (i=0;i<100;i++)result[i]=0;
for (i=length-1;i>=0;i--)
if (savename[i]=='\\') break;
j=0;
for (j=0;j<=i;j++)result[j]=savename[j];
int k=0;
while (k
{
result[j]=a[k];
j++;k++;
}
}
long strl(char *s)
{
long i=0;
for (i=0;s[i]!='\0';i++);
return i;
}
c语言删除一个字符指令,【C语言】实现一个基于命令行的文本编辑器相关推荐
- c语言编程实现二进制计算器,本程序是用纯C语言编的一个基于命令行的四则运算计算器。主要用于计算四则运算表达式的值,同时可以实现四...
本程序是用纯C语言编的一个基于命令行的四则运算计算器.主要用于计算四则运算表达式的值,同时可以实现四 2016-08-22 0 0 0 暂无评分 其他 1 积分下载 如何获取积分? 本程序是用纯C语言 ...
- 计算器四则运算c语言,C语言:基于命令行的四则运算计算器
题目: C语言:基于命令行的四则运算计算器 功能要求: 四则运算就是包含+.-.*./..(小数点)和数字的运算表达式,例如:3+2.9*(5-6/3)等 能够解释并执行四则运算表达式 能 ...
- shell 获取字符串前两个字符串、获取字符串最后一个字符、去掉字符串最后一个字符、去掉末尾一个字符、去掉末尾两个字符
1. 获取字符串前两个字符串 temp=`echo $RANDOM|md5sum|sed 's/../&:/g'|cut -c 1-17` echo $temp echo ${temp:0:2 ...
- windows winrar 指令_【转】winrar命令行详解
从命令行也可以运行 WinRAR 命令,常规的命令行语法描述如下: WinRAR - - 命令:WinRAR 运行的字符组合代表功能 开关:切换操作指定类型,压缩强度,压缩文件类型,等等的定义. 压 ...
- java输入字符串异常_设计一个 Java 程序,自定义异常类,从命令行(键盘)输入一个字符串,如果该字符串值为“XYZ”。。。...
设计一个 Java 程序,自定义异常类,从命令行(键盘)输入一个字符串,如果该字符串值为"XYZ",则抛出一个异常信息"This is a XYZ",如果从命令 ...
- Redis Gli - 一个基于命令行的 Redis 图形界面客户端
2019独角兽企业重金招聘Python工程师标准>>> 很久之前在 Github 上发现了一个用 Golang 来实现的命令行图形界面库,一直想用来做点东西. https://git ...
- c语言中减号算一个字符吗,C语言中指针的加减运算
char arr[3]; printf("arr:\n%d\n%d\n%d\n", arr, arr + 1, arr + 2); char *parr[3]; printf(&q ...
- C语言怎么判断字符YN,c语言中的宏_详解(转)
1. 简单宏定义 简单的宏定义有如下格式: [#define指令(简单的宏)] #define 标识符替换列表 替换列表是一系列的C语言记号,包括标识符.关键字.数.字符常量.字符串字面量.运算符和标 ...
- c语言设置输出字符大小_C语言中常用的几个头文件及库函数
点击上方"C语言中文社区",选择"设为星标★" 技术干货第一时间送达! 来源:https://www.jb51.net/article/124594.htm 这 ...
最新文章
- GIT:本地有更改,但强制作远程仓库里作更新
- ICMP最典型的应用PING和traceroute
- C++中四种 cast 转换
- MFC下CSocket编程详解
- java 柱状图jar_GitHub - mafulong/NetworkExper: 计网实验,抓包,java,jigloo界面开发,柱状图,文件自定义保存...
- [转]VB中资源文件.res的使用方法详解
- 帝国cms index.php?id=调不到指定文章,帝国cms修改实现TAG标签以TAGID的方式伪静态...
- [转]二阶巴特沃斯(Butterworth)滤波器
- 离散数学:用python实现矩阵乘法与关系矩阵
- Windows下获取群CPU使用率的方法
- Java - 使用Cipher类实现加密(RSA)
- “打开文件所在位置”提示“找不到应用程序”的解决方案
- 【selenium标签页操作】:关闭标签页
- 【MyBatis】缓存——使查询变得快快快!
- 数据库入门理论知识介绍以及编译安装MySql
- ARM9嵌入式Linux开发-内存与IO操作
- 比前途,还是嵌入式开发比软件开发更胜一筹
- VulnStack-ATTCK-3(红日靶场三)
- guzzle下载图片
- Praat脚本-022 | 提取时长和音强
热门文章
- 万字通俗讲解何为复杂度
- 等保数据备份和恢复关键点,这些你该知道!
- RDS、DDS和GaussDB理不清?看这一篇足够了!
- 【华为云技术分享】华为云HiLens全面升级,端云协同多模态AI应用开发利器
- 解密昇腾AI处理器--DaVinci架构(总览)
- Python小数据保存,有多少中分类?不妨看看他们的类比与推荐方案...
- Spring MVC DispatcherServlet改造为 CSE RestServlet 常见问题汇编
- ssh隧道 mysql,如何通过SSH隧道连接MySQL
- linux开放mysql远程连接_Linux开启MySql远程连接
- kohana php,[php框架]kohana中文译本.pdf