| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 574 人关注过本帖
标题:[求助] 简单编辑器 的错误在那?
只看楼主 加入收藏
z331499778
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-2-5
收藏
 问题点数:0 回复次数:1 
[求助] 简单编辑器 的错误在那?

#include<stdio.h>
#include<stdlib.h>
#define max_len 25
#define line_len 50
#define name_len 20
char filename[name_len]=" ";
char str[line_len];
FILE *fp;
int n=0;

typedef struct
{char line[line_len];}*linelist;

linelist *line[max_len];

void initstring(char t[line_len])
{int i;
for(i=0;i<line_len;i++)
t[i]=NULL;
}

void strassign(char t[line_len],char s[line_len])
{int i;
for(i=0;i<line_len;i++)
t[i]=s[i];
}

void open( )
{
void strassign(char t[line_len],char s[line_len]);
int i,j;
if(filename[0])
printf("the file has been opened\n");
else
{
printf("please input the file name(no more than %d characters):",name_len-1);
scanf("%s",filename);
fp=fopen(filename,"r");
if(fp)
{
do
{
fgets(str,line_len,fp);
i=strlen(str);
str[i-1]=NULL;
i--;
if(i>0)
{
j=n;
strassign(line[j],str);
j++;
n=j;
if(n>max_len)
printf("the file is too large!\n");
}
}while(i>0);
fclose(fp);
}
else
printf("new file\n");
}
}


void strprint(char t[line_len])
{printf("%s",t);}


void list( )
{
int i;
for(i=0;i<n;i++)
{
printf("%d: ",i+1);
strprint(line[i]);
}
getchar( );
}

void insert( )
{
int i,l,m;
printf("insert how many lines before which line,input the number:");
scanf("%d%d",&l,&m);
if(n+m>max_len)
printf("the inserted lines are too much\n");
if(n>=l-1&&l>0)
{
for(i=n-1;i>=l-1;i--)
line[i+m]=line[i];
n+=m;
printf("please input the insert content in order:\n");
for(i=l-1;i<l-1+m;i++)
{
gets(str);
initstring(line[i]);
strassign(line[i],str);
}
}
else
printf("the lines are overflow!\n");
}


void delete( )
{
int i,l,m;
printf("from which line for deleting how many lines,input the number:");
scanf("%d%d",&l,&m);
if(n>=l+m-1&&l>0)
{
for(i=l-1+m;i<n;i++)
line[i-m]=line[i];
for(i=n-m;i<n;i++)
initstring(line[i]);
n-=m;
}
else
printf("the lines are overflow!\n");
}

void copy( )
{
int i,l,m,k;
printf("from which line to copy how many lines and insert them before which line,\ninput the number:\n");
scanf("%d%d%d",&l,&m,&k);
if(n+m>max_len)
printf("the lines for copying are too much!\n");
if(n>=k-1&&n>=l-1+m&&(k>=l+m||k<=l))
{
for(i=n-1;i>=k-1;i--)
line[i+m]=line[i];
n+=m;
if(k<=l)
l+=m;
for(i=l-1;i<l-1+m;i++)
{
initstring(line[i+k-l]);
strcpy(line[i+k-1],line[i]);
}
}
else
printf("the lines are overflow!\n");
}


void modify( )
{
int i;
printf("input the line number which you will modify:");
scanf("%d",&i);
if(i>0&&i<=n)
{
printf("%d: ",i);
strprint(line[i-1]);
printf("input the new content: ");
gets(str);
strassign(line[i-1],str);
}
else
printf("the line number are overflow\n");
}

void substring(char s[line_len],char t[line_len],int pos,int len)
{int i,m;
m=strlen(t);
if(pos<1||pos>m||len<0||len>m-pos+1)
printf("error!\n");
for(i=1;i<=len;i++)
s[i]=s[pos+i-1];
}

int index(char s[line_len],char t[line_len],int pos)
{int m,p,i;
char sub[line_len];
if(pos>0)
{m=strlen(t);
p=strlen(s);
i=pos;
while(i<=p-m+1)
{
substring(sub,s,i,m);
if(strcmp(sub,t)!=0)
++i;
else return i;
}
}
return 0;
}

void search()
{
int i,k,f=1;
char b;
char s[line_len];
printf("input the string you search for: ");
scanf("%s",str);
initstring(s);
strassign(s,str);
for(i=0;i<n&&f;i++)
{
k=1;
while(k)
{
k=index(line[i],s,k);
if(k)
{
printf("the %d line: ",i+1);
strprint(line[i]);
printf("the %d character is found.will you continue to search for?(Y/N)? ",k);
b=getchar();
getchar();
if(b!='Y'&&b!='y')
{
f=0;
break;
}
else
k++;
}
}
}
if(f)
printf("the character isn't found\n");
}


void strdelete(char s[line_len],int pos,int len)
{int i;
if(pos<1||pos>strlen(s)+1)
printf("error!\n");
for(i=pos+len;i<=strlen(s);i++)
s[i-len]=s[i];
}

void strinsert(char s[line_len],int pos,char t[line_len])
{
int i;
if(pos<1||pos>strlen(s)+1)
printf("error!\n");
if(strlen(s)+strlen(t)<=line_len)
{
for(i=strlen(s);i>=pos;i--)
s[i+strlen(t)]=s[i];
for(i=pos;i<pos+strlen(t);i++)
s[i]=t[i-pos+1];
printf("inert completly!\n");
}
else
{
for(i=line_len;i<=pos;i--)
s[i]=s[i-strlen(t)];
for(i=pos;i<pos+strlen(t);i++)
s[i]=t[i-pos+1];
printf("insert not completly!\n");
}
}


void replace()
{
int i,k,f=1;
char b;
char s[line_len],t[line_len];
printf("input the string you want to replace: ");
scanf("%s",str);
initstring(s);
strassign(s,str);
printf("replace for: ");
scanf("%s%*c",str);
initstring(t);
strassign(t,str);
for(i=0;i<n&&f;i++)
{
k=1;
while(k)
{
k=index(line[i],s,k);
if(k)
{
printf("the %d line: ",i+1);
strprint(line[i]);
printf("the %d character is found.do you want to replace it(Y/N)? ",k);
b=getchar();
getchar();
if(b=='Y'||b=='y')
{
strdelete(line[i],k,strlen(s));
strinsert(line[i],k,t);
}
printf("contiue to replace?(Y/N)?");
b=getchar();
getchar();
if(b!='Y'&&b!='y')
{
f=0;
break;
}
else
k+=strlen(t);
}
}
}
if(f)
printf("not found\n");
}

void main( )
{
int i,k;
int loop;
loop=1;
for(i=0;i<max_len;i++)
initstring(line[i]);
while(loop)
{
printf("make your choice:\n");
printf("1.open the file(new or old) 2.list the content of file\n");
printf("3.insert line 4.delete line\n");
printf("5.copy line 6.modify line\n");
printf("7.search string 8.replace string\n");
printf("9.give up editing 0.exit\n");
scanf("%d",&k);
switch(k)
{
case 1: open();
break;
case 2: list();
break;
case 3: insert();
break;
case 4: delete();
break;
case 5: copy();
break;
case 6: modify();
break;
case 7: search();
break;
case 8: replace();
break;
case 9: loop=0;
case 0:exit(0);
}
}
}





文件打不开!
请教高手!

搜索更多相关主题的帖子: 编辑器 
2006-11-13 11:11
perfect
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:81
专家分:0
注 册:2006-11-19
收藏
得分:0 

void initstring(char t[line_len]) 应该是 char *t[]
{int i;
for(i=0;i<line_len;i++)
t[i]=NULL;
}

if(filename[0])
中的filename[0]是空格,并不是'\0'
没有打开的文件也会显示the file has been opened

printf("9.give up editing 0.exit\n");
放弃编辑,那么以前的修改怎么办?
直接退出循环更好一点吧


片言可以明百意 坐驰可以役万里
2006-11-19 12:21
快速回复:[求助] 简单编辑器 的错误在那?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034923 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved