在这里插入代码片
```//行编辑器
#include
#include
#include
#define MAXLEN 256 //每行存储字符数
void openFile();
void closeFile();
void saveFile();
void listLines();
void gotoLine();
void insertString();
void deleteString();
void showHelp();
void countLine();
void enterFileNmae();
void clearBuffer();
void interPosition(int *,int *);
char fname[256]="";
char (*buf)[MAXLEN]=NULL;
int lines=0;
//菜单
int main()
{
char cmd[20];
while(1)
{
printf("comand:");
scanf("%s",cmd);
if(strcmp(cmd,"fopen")==0) openFile();
else if(strcmp(cmd,"fcolse")==0) closeFile();
else if(strcmp(cmd,"fsave")==0) saveFile();
else if(strcmp(cmd,"list")==0) listLines();
else if(strcmp(cmd,"gotoln")==0) gotoLine();
else if(strcmp(cmd,"inss")==0) insertString();
else if(strcmp(cmd,"dels")==0) deleteString();
else if(strcmp(cmd,"help")==0) showHelp();
else if(strcmp(cmd,"exit")==0) break;
else printf("%s?n",cmd); //输入错误命令时处理;
}
clearBuffer();
return 0;
}
//文件名输入
void enterFileNmae()
{
printf("filename:");
scanf("%s",fname);
}
//文件中数据行的数计算
void countLines()
{
FILE *fp;
char s[MAXLEN];
lines=0;
fp=fopen(fname,"r");//读取模式
if(fp==NULL) return ;
while(fgets(s,MAXLEN-1,fp))
{
lines++;
printf("m=%dn",lines);
fclose(fp);
}
}
//指针变量,文件名以及行数的初始化
void clearBuffer()
{
fname[0]=' ';//文件名是空的
lines=0;
if(buf) free(buf);
buf=NULL;
}
//分配内存,读入文件
void openFile()
{
int n=0;
FILE *fp;
char *myline;
enterFileNmae();
countLines();
//countLine();
buf=(char(*)[MAXLEN])malloc(sizeof(char) * lines * MAXLEN);
if(!buf) return ;
fp=fopen(fname,"r");
if(fp=NULL)
{
clearBuffer();
return ;
}
while(fgets(buf[n],MAXLEN-1,fp))
{
myline=buf[n];//数据复制到缓存单元中
myline[strlen(myline-1)]=' ';
n++;
}
fclose(fp);
printf("读入%d行。n",lines);
}
//内存释放
void closeFile()
{
char ans[20]=" ";
printf("是否保存?(Y or N)");
scanf("%s",ans);
if(ans[0]=='y') saveFile();
clearBuffer();
printf("以释放内存。n");
}
//保存文件
void saveFile()
{
int n;
FILE *fp;
enterFileNmae(); //input the file name;
fp=fopen(fname,"w") ; //writing
if(fp==NULL) return ;
for(n=0;n= lines) //检查变量row的数值范围。
return;
characters = strlen(buf[row]);
printf("位数:");
scanf("%d", &col);
if(col <0 ||col >= characters) //检查变量co1的数值范围。
return;
*prow = row;
*pcol = col;
}
//字符串的插入
void insertString()
{
int row, col; //行号.位数
char insstr[MAXLEN]; //待插入的字符串
char newstr [MAXLEN]; //插入后的字符串
char *myline; //目标行
interPosition(&row, &col);
myline = buf[row];
printf("插入字符串:");
scanf("&s",insstr);
if(strlen(myline) + strlen(insstr) >= MAXLEN)
return;
sprintf (newstr, "%s%s",insstr, myline + col);
strcpy (myline + col, newstr);
printf("第&d行第&d个字符后插入"&s"n", row, col, insstr);
printf("&04d:8sn", row, myline);
}
//字符串的删除
void deleteString()
{
int row, col;
//行号,位数
int delnum;
//删除的字符串数
char *myline; //目标行
int mylength; // 目标行字符串的长度
int i ; //计数器
//interPosition(int *prow,int *pcol);
interPosition(&row, &col);
myline = buf[ row];
mylength = strlen (myline);
printf("字符数:");
scanf("&d", &delnum);
if(delnum <= 0 || mylength < col + delnum) //检查删除字符数的数值范围。
return;
for(i = col; i <= mylength - delnum; i++) //删除指定字符。
myline[i] = myline[i + delnum];
printf("从第%d行的第%d个字符后开始删除%d个字符。n", row, col,delnum);
printf("%04d:%sn",row,myline);//行,线)
}
//帮助信息的显示
void showHelp()
{
printf(" COMMAND HELP :"help"n");
printf( "LOAD FILE :"fopen"n") ;
printf("RELEASE BUFFER :"fclose"n");
printf("SAVE FILE :"fsave"n");
printf("SHOW ALL LINES :"list"n");
printf("SHOW SINGLE LINE :"gotoln"n");
printf ("INSERT STRING :"inss"n");
printf("DELETE STRING :"dels"n");
}