📄 行编辑器.cpp
字号:
#include"string.h"
#include"stdlib.h"
#include"stdio.h"
void write(void);
void read(void);
void search(void);
int replace(char str[],char st[],int n);
void add(void);
int main()
{
int n=10;
char str[10],st[10];
while(1)
{
printf("\nStart your program:\n");
printf("input 'w' or 'W' to write the strings,\n");
printf("input 'r' or 'R' to read the stings,\n");
printf("input 's' or 'S' to search the string,\n");
printf("input 'p' or 'P' to replace the strings,\n");
printf("input 'c' or 'C' to correct the strings.else,end.\n");
char ch;
ch=getchar();
switch(ch)
{
case'w':
case'W':
printf("input the strings:\n");
write();
break;
case'r':
case'R':
printf("read the strings:\n\n");
read();
getchar();
break;
case's':
case'S':
printf("\nsearch the strings:\n");
search();
break;
case'p':
case'P':
printf("\nreplace the strings.\n");
replace(str,st,n);
break;
case'a':
case'A':
printf("\ncorrect the strings:\n");
add();
break;
default:
printf("\nEnd this program.\n");
break;
}
}
return(0);
}
void write(void)
{
FILE *fp;
char ch;
char string[50];
if((fp=fopen("file2.txt","w"))==NULL)
{
printf("cannot open this file.");
exit(0);
}
ch = getchar();
gets(string);
do
{
fputs(string,fp);
fputs("\n",fp);
}while(strlen(gets(string))>0);
printf("the file has been written.\n");
fclose(fp);
}
void read(void)
{
FILE *fp;
char string[50];
char * pBuf;
char ch;
if((fp=fopen("file2.txt","r"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
ch = getchar();
while((pBuf=fgets(string,10,fp))!=NULL)
printf("%s",string);
fclose(fp);
}
void search(void)
{
FILE *fp;
char string[50],str[10];
char ch;
char *pbuf;
char *pp;
if((fp=fopen("file2.txt","w"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
ch = getchar();
gets(str);
if((pp=fgets(string,50,fp))!=NULL)
{
if((pbuf = strstr(string,str))!=NULL)
printf("the strings you search is exist.\n\n");
printf("the strings you search is not exist\n\n");
}
fclose(fp);
}
int replace(char str[],char st[],int n)
{
FILE *fp;
char string[50];
char *pbuf;
char *pp;
char ch;
ch=getchar();
gets(str);
if((fp=fopen("file2.txt","w"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
while((pp=fgets(string,50,fp))!=NULL)
{
if((pbuf=strstr(string,str))!=NULL)
{
printf("input the strings you want to replace:");
gets(st);
strcpy(str,st);
printf("\n");
read();
}
else
printf("the strings you search is not exist\n");
}
fclose(fp);
return(0);
}
void add(void)
{
FILE *fp;
char string[50],str[10],st[10];
getchar();
gets(str);
if((fp=fopen("file2.txt","w"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
while(strstr(string,str)!=NULL)
{
printf("the strings you search is exist");
printf("then input strings you want to add:");
gets(st);
strcat(str,st);
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -