Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.25 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define MAX 4095
  5.  
  6. int newtxt(char *filename);
  7. int oldtxt(char *filename);
  8. int oldprint(char *filename);
  9. int addingmode(char *filename);
  10. int copy(char *filename);
  11.  
  12. int main(void)
  13. {
  14.     FILE *f;
  15.     char filename[MAX]={'\0'};
  16.  
  17.     printf("\t\t|||This is a notepad programming|||\n");
  18.     printf("User's guide:1.Enter character '#' and pressing enter to end the notepad.\n");
  19.     printf("\t     2.Enter character '+' and pressing enter to use the adding mode.\n");
  20.     printf("\t     3.Enter character '-' and pressing enter to use the deleting mode.\n");
  21.     printf("\t     4.The editing mode(add and delete) can not be use constantly!!!\n");
  22.     printf("(When exiting the editing mode,you must go to the original mode one time!)\n");
  23.     printf("P.S. If you want to edit the n-th character and the n-th character is located\n");
  24.     printf("     in the k-th row,the number you must enter is n plus (k-1).\n");
  25.  
  26.     printf("\nPlease enter the filename:");
  27.     scanf("%s",filename);
  28.  
  29.     printf("Loading <%s>\n",filename);
  30.  
  31.     if((f=fopen(filename,"r+"))==NULL)
  32.     {
  33.         printf("<%s> has not been found!\nCreate a new file!\n",filename);
  34.         newtxt(filename);
  35.     }
  36.  
  37.     else
  38.     {
  39.         printf("<%s> has been opened successfully!\n",filename);
  40.         oldprint(filename);
  41.         oldtxt(filename);
  42.     }
  43.  
  44.     return 0;
  45. }
  46.  
  47. int newtxt(char *filename)
  48. {
  49.     FILE *fo,*fp;
  50.     char key={'\0'};
  51.     int i;
  52.  
  53.     fo=fopen(filename,"w+");
  54.     fp=fopen("notepad.txt","w+");
  55.  
  56.     if(fp==NULL)
  57.     {
  58.         printf("The new file can not be opened!\n");
  59.         exit(1);
  60.     }
  61.  
  62.     printf("Please enter the data:\n");
  63.  
  64.     getchar();
  65.  
  66.     do
  67.     {
  68.         scanf("%c",&key);
  69.  
  70.         fprintf(fp,"%c",key);
  71.     }while(key!='#');
  72.  
  73.     fprintf(fp,"%c",'\n');
  74.     printf("The data has been saved successfully!\n");
  75.  
  76.     fclose(fo);
  77.     fclose(fp);
  78.  
  79.     return 0;
  80. }
  81.  
  82. int oldtxt(char *filename)
  83. {
  84.     FILE *fo,*fp;
  85.     char key={'\0'};
  86.  
  87.     fo=fopen(filename,"r+");
  88.     fp=fopen("notepad.txt","a+");
  89.  
  90.     if(fo==NULL)
  91.     {
  92.         printf("The old file can not be opened!\n");
  93.         exit(1);
  94.     }
  95.  
  96.     if(fp==NULL)
  97.     {
  98.         printf("The new file can not be opened!\n");
  99.         exit(1);
  100.     }
  101.  
  102.     printf("Please enter the data:\n");
  103.  
  104.     getchar();
  105.  
  106.     do
  107.     {
  108.         scanf("%c",&key);
  109.  
  110.         if(key=='+')
  111.         {
  112.             getchar();
  113.  
  114.             printf("~~~Enter the adding mode~~~\n");
  115.             addingmode(filename);
  116.             key='\0';
  117.             copy(filename);
  118.             printf("~~~Exit the adding mode~~~\n");
  119.  
  120.             fclose(fo);
  121.             fclose(fp);
  122.             oldprint(filename);
  123.  
  124.             fo=fopen(filename,"r+");
  125.             fp=fopen("notepad.txt","a+");
  126.  
  127.             printf("Please enter the data:\n");
  128.             scanf("%c",&key);
  129.         }
  130.  
  131.         if(key=='-')
  132.         {
  133.             getchar();
  134.  
  135.             printf("~~~Enter the deleting mode~~~\n");
  136.             deletingmode(filename);
  137.             key='\0';
  138.             copy(filename);
  139.             printf("~~~Exit the deleting mode~~~\n");
  140.  
  141.             fclose(fo);
  142.             fclose(fp);
  143.             oldprint(filename);
  144.  
  145.             fo=fopen(filename,"r+");
  146.             fp=fopen("notepad.txt","a+");
  147.  
  148.             printf("Please enter the data:\n");
  149.             scanf("%c",&key);
  150.         }
  151.  
  152.         fprintf(fp,"%c",key);
  153.  
  154.     }while(key!='#');
  155.  
  156.     fprintf(fp,"%c",'\n');
  157.     printf("The data has been saved successfully!\n");
  158.  
  159.     fclose(fo);
  160.     fclose(fp);
  161.  
  162.     return 0;
  163. }
  164.  
  165. int oldprint(char *filename)
  166. {
  167.     FILE *file;
  168.     char array[MAX]={'\0'};
  169.  
  170.     file=fopen("notepad.txt","r+");
  171.  
  172.     printf("--------------------------------------------------------------------------------");
  173.  
  174.     if (file==NULL)
  175.     {
  176.         printf("Fail to open!");
  177.         exit(1);
  178.     }
  179.  
  180.     else
  181.     {
  182.         fread(array,MAX,1,file);
  183.         printf("%s",array);
  184.     }
  185.  
  186.     printf("--------------------------------------------------------------------------------");
  187.  
  188.     fclose(file);
  189.  
  190.     return 0;
  191. }
  192.  
  193. int addingmode(char *filename)
  194. {
  195.     FILE *fo,*fp;
  196.     char before[MAX]={'\0'},after[MAX]={'\0'},buffer[80]={'\0'},c={'\0'};
  197.     int k;
  198.  
  199.     fo=fopen("notepad.txt","r+");
  200.     fp=fopen("modify.txt","w+");
  201.  
  202.     if (fo==NULL)
  203.     {
  204.         printf("Fail to open!");
  205.         exit(1);
  206.     }
  207.  
  208.     else if (fp==NULL)
  209.     {
  210.         printf("Fail to open!");
  211.         exit(1);
  212.     }
  213.  
  214.     else
  215.     {
  216.         printf("Enter the editing letter's location(from the first word to count):");
  217.         scanf("%d",&k);
  218.         getchar();
  219.         printf("Enter the letter you want to replace:");
  220.         scanf("%c",&c);
  221.         getchar();
  222.  
  223.         rewind(fo);
  224.         fread(before,k-1,1,fo);
  225.         fgets(buffer,80,fo);
  226.         fread(after,MAX,1,fo);
  227.  
  228.         fprintf(fp,"%s",before);
  229.         fprintf(fp,"%c",c);
  230.         fprintf(fp,"%s",buffer);
  231.         fprintf(fp,"%s",after);
  232.     }
  233.  
  234.     fclose(fo);
  235.     fclose(fp);
  236.  
  237.     return 0;
  238. }
  239.  
  240. int deletingmode(char *filename)
  241. {
  242.     FILE *fo,*fp;
  243.     char before[MAX]={'\0'},after[MAX]={'\0'};
  244.     int k;
  245.  
  246.     fo=fopen("notepad.txt","r+");
  247.     fp=fopen("modify.txt","w+");
  248.  
  249.     if (fo==NULL)
  250.     {
  251.         printf("Fail to open!");
  252.         exit(1);
  253.     }
  254.  
  255.     else if (fp==NULL)
  256.     {
  257.         printf("Fail to open!");
  258.         exit(1);
  259.     }
  260.  
  261.     else
  262.     {
  263.         printf("Enter the editing letter's location(from the first word to count):");
  264.         scanf("%d",&k);
  265.         getchar();
  266.  
  267.         rewind(fo);
  268.         fread(before,k-1,1,fo);
  269.  
  270.         fseek(fo,1,SEEK_CUR);
  271.         fread(after,MAX,1,fo);
  272.  
  273.         fprintf(fp,"%s",before);
  274.         fprintf(fp,"%s",after);
  275.     }
  276.  
  277.     fclose(fo);
  278.     fclose(fp);
  279.  
  280.     return 0;
  281. }
  282.  
  283. int copy(char *filename)
  284. {
  285.     FILE *fo,*fp;
  286.     char array[MAX]={'\0'};
  287.     int i,j;
  288.  
  289.     fo=fopen("notepad.txt","w+");
  290.     fp=fopen("modify.txt","r+");
  291.  
  292.     if (fo==NULL)
  293.     {
  294.         printf("Fail to open!");
  295.         exit(1);
  296.     }
  297.  
  298.     else if (fp==NULL)
  299.     {
  300.         printf("Fail to open!");
  301.         exit(1);
  302.     }
  303.  
  304.     fread(array,MAX,1,fp);
  305.     fprintf(fo,"%s",array);
  306.  
  307.     fclose(fo);
  308.     fclose(fp);
  309.  
  310.     return 0;
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement