Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 4095
- int newtxt(char *filename);
- int oldtxt(char *filename);
- int oldprint(char *filename);
- int addingmode(char *filename);
- int copy(char *filename);
- int main(void)
- {
- FILE *f;
- char filename[MAX]={'\0'};
- printf("\t\t|||This is a notepad programming|||\n");
- printf("User's guide:1.Enter character '#' and pressing enter to end the notepad.\n");
- printf("\t 2.Enter character '+' and pressing enter to use the adding mode.\n");
- printf("\t 3.Enter character '-' and pressing enter to use the deleting mode.\n");
- printf("\t 4.The editing mode(add and delete) can not be use constantly!!!\n");
- printf("(When exiting the editing mode,you must go to the original mode one time!)\n");
- printf("P.S. If you want to edit the n-th character and the n-th character is located\n");
- printf(" in the k-th row,the number you must enter is n plus (k-1).\n");
- printf("\nPlease enter the filename:");
- scanf("%s",filename);
- printf("Loading <%s>\n",filename);
- if((f=fopen(filename,"r+"))==NULL)
- {
- printf("<%s> has not been found!\nCreate a new file!\n",filename);
- newtxt(filename);
- }
- else
- {
- printf("<%s> has been opened successfully!\n",filename);
- oldprint(filename);
- oldtxt(filename);
- }
- return 0;
- }
- int newtxt(char *filename)
- {
- FILE *fo,*fp;
- char key={'\0'};
- int i;
- fo=fopen(filename,"w+");
- fp=fopen("notepad.txt","w+");
- if(fp==NULL)
- {
- printf("The new file can not be opened!\n");
- exit(1);
- }
- printf("Please enter the data:\n");
- getchar();
- do
- {
- scanf("%c",&key);
- fprintf(fp,"%c",key);
- }while(key!='#');
- fprintf(fp,"%c",'\n');
- printf("The data has been saved successfully!\n");
- fclose(fo);
- fclose(fp);
- return 0;
- }
- int oldtxt(char *filename)
- {
- FILE *fo,*fp;
- char key={'\0'};
- fo=fopen(filename,"r+");
- fp=fopen("notepad.txt","a+");
- if(fo==NULL)
- {
- printf("The old file can not be opened!\n");
- exit(1);
- }
- if(fp==NULL)
- {
- printf("The new file can not be opened!\n");
- exit(1);
- }
- printf("Please enter the data:\n");
- getchar();
- do
- {
- scanf("%c",&key);
- if(key=='+')
- {
- getchar();
- printf("~~~Enter the adding mode~~~\n");
- addingmode(filename);
- key='\0';
- copy(filename);
- printf("~~~Exit the adding mode~~~\n");
- fclose(fo);
- fclose(fp);
- oldprint(filename);
- fo=fopen(filename,"r+");
- fp=fopen("notepad.txt","a+");
- printf("Please enter the data:\n");
- scanf("%c",&key);
- }
- if(key=='-')
- {
- getchar();
- printf("~~~Enter the deleting mode~~~\n");
- deletingmode(filename);
- key='\0';
- copy(filename);
- printf("~~~Exit the deleting mode~~~\n");
- fclose(fo);
- fclose(fp);
- oldprint(filename);
- fo=fopen(filename,"r+");
- fp=fopen("notepad.txt","a+");
- printf("Please enter the data:\n");
- scanf("%c",&key);
- }
- fprintf(fp,"%c",key);
- }while(key!='#');
- fprintf(fp,"%c",'\n');
- printf("The data has been saved successfully!\n");
- fclose(fo);
- fclose(fp);
- return 0;
- }
- int oldprint(char *filename)
- {
- FILE *file;
- char array[MAX]={'\0'};
- file=fopen("notepad.txt","r+");
- printf("--------------------------------------------------------------------------------");
- if (file==NULL)
- {
- printf("Fail to open!");
- exit(1);
- }
- else
- {
- fread(array,MAX,1,file);
- printf("%s",array);
- }
- printf("--------------------------------------------------------------------------------");
- fclose(file);
- return 0;
- }
- int addingmode(char *filename)
- {
- FILE *fo,*fp;
- char before[MAX]={'\0'},after[MAX]={'\0'},buffer[80]={'\0'},c={'\0'};
- int k;
- fo=fopen("notepad.txt","r+");
- fp=fopen("modify.txt","w+");
- if (fo==NULL)
- {
- printf("Fail to open!");
- exit(1);
- }
- else if (fp==NULL)
- {
- printf("Fail to open!");
- exit(1);
- }
- else
- {
- printf("Enter the editing letter's location(from the first word to count):");
- scanf("%d",&k);
- getchar();
- printf("Enter the letter you want to replace:");
- scanf("%c",&c);
- getchar();
- rewind(fo);
- fread(before,k-1,1,fo);
- fgets(buffer,80,fo);
- fread(after,MAX,1,fo);
- fprintf(fp,"%s",before);
- fprintf(fp,"%c",c);
- fprintf(fp,"%s",buffer);
- fprintf(fp,"%s",after);
- }
- fclose(fo);
- fclose(fp);
- return 0;
- }
- int deletingmode(char *filename)
- {
- FILE *fo,*fp;
- char before[MAX]={'\0'},after[MAX]={'\0'};
- int k;
- fo=fopen("notepad.txt","r+");
- fp=fopen("modify.txt","w+");
- if (fo==NULL)
- {
- printf("Fail to open!");
- exit(1);
- }
- else if (fp==NULL)
- {
- printf("Fail to open!");
- exit(1);
- }
- else
- {
- printf("Enter the editing letter's location(from the first word to count):");
- scanf("%d",&k);
- getchar();
- rewind(fo);
- fread(before,k-1,1,fo);
- fseek(fo,1,SEEK_CUR);
- fread(after,MAX,1,fo);
- fprintf(fp,"%s",before);
- fprintf(fp,"%s",after);
- }
- fclose(fo);
- fclose(fp);
- return 0;
- }
- int copy(char *filename)
- {
- FILE *fo,*fp;
- char array[MAX]={'\0'};
- int i,j;
- fo=fopen("notepad.txt","w+");
- fp=fopen("modify.txt","r+");
- if (fo==NULL)
- {
- printf("Fail to open!");
- exit(1);
- }
- else if (fp==NULL)
- {
- printf("Fail to open!");
- exit(1);
- }
- fread(array,MAX,1,fp);
- fprintf(fo,"%s",array);
- fclose(fo);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement