选择本项目作业的同学,必须本人独立完成。
功能:
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;
}