Advertisement
katbiral

Text Editor

Apr 14th, 2021
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<process.h>
  4. int i,j,ch;
  5. char fn[20],e,c;
  6. FILE *fp1,*fp2,*fp;
  7. void Create();
  8. void Append();
  9. void Copy();
  10. void Delete();
  11. void Display();
  12. void main()
  13. {
  14. do {
  15. clrscr();
  16. printf("\n\t\t***** TEXT EDITOR *****");
  17. printf("\n\n\tMENU:\n\t\n");
  18. printf("\n\t1.CREATE\n\t2.DISPLAY\n\t3.APPEND\n\t4.COPY\n\t5.DELETE\n\t6.EXIT\n");
  19. printf("\n\tEnter your choice: ");
  20. scanf("%d",&ch);
  21. switch(ch)
  22. {
  23. case 1:
  24. Create();
  25. break;
  26. case 2:
  27. Display();
  28. break;
  29. case 3:
  30. Append();
  31. break;
  32. case 4:
  33. Copy();
  34. break;
  35. case 5:
  36. Delete();
  37. break;
  38. case 6:
  39. exit(0);
  40. }
  41. }while(1);
  42. }
  43. void Create()
  44. {
  45. fp1=fopen("temp.txt","w");
  46. printf("\n\tEnter the text and press '.' to save\n\n\t");
  47. while(1)
  48. {
  49. c=getchar();
  50. fputc(c,fp1);
  51. if(c == '.')
  52. {
  53. fclose(fp1);
  54. break;
  55. }
  56. }}
  57.  
  58. void Display()
  59. {
  60. printf("\n\tEnter the file name: ");
  61. scanf("%s",fn);
  62. fp1=fopen(fn,"r");
  63. if(fp1==NULL)
  64. {
  65. printf("\n\tFile not found!");
  66. goto end1;
  67. }
  68. while(!feof(fp1))
  69. {
  70. c=getc(fp1);
  71. printf("%c",c);
  72. }
  73. end1:
  74. fclose(fp1);
  75. printf("\n\n\tPress any key to continue\n");
  76. getch();
  77. }
  78. void Copy()
  79. {
  80. printf("\n\tEnter the new filenameto copy:  ");
  81. scanf("%s",fn);
  82. fp1=fopen("temp.txt","r");
  83. fp2=fopen(fn,"w");
  84. while(!feof(fp1))
  85. {
  86. c=getc(fp1);
  87. putc(c,fp2);
  88. }
  89. fclose(fp2);
  90. }
  91.  
  92. void Delete()
  93. {
  94. printf("\n\tEnter the file name: ");
  95. scanf("%s",fn);
  96. fp1=fopen(fn,"r");
  97. if(fp1==NULL)
  98. {
  99. printf("\n\tFile not found!");
  100. goto end2;
  101. }
  102. fclose(fp1);
  103. if(remove(fn)==0)
  104. {
  105. printf("\n\n\tFile has been deleted successfully!");
  106. goto end2;
  107. }
  108. else
  109. printf("\n\tError!\n");
  110. end2: printf("\n\n\tPress any key to continue\n");
  111. getch();
  112. }
  113.  
  114. void Append()
  115. {
  116. printf("\n\tEnter the file name: ");
  117. scanf("%s",fn);
  118. fp1=fopen(fn,"r");
  119. if(fp1==NULL)
  120. {
  121. printf("\n\tFile not found!");
  122. fclose(fp1);
  123. goto end3;
  124. }
  125. while(!feof(fp1))
  126. {
  127. c=getc(fp1);
  128. printf("%c",c);
  129. }
  130. fclose(fp1);
  131. printf("\n\tType the text and press Ctrl+S to append.\n");
  132. fp1=fopen(fn,"a");
  133. while(1)
  134. {
  135. c=getch();
  136. if(c==19)
  137. goto end3;
  138. if(c==13)
  139. {
  140. c='\n';
  141. printf("\n\t");
  142. fputc(c,fp1);
  143. }
  144. else
  145. {
  146. printf("%c",c);
  147. fputc(c,fp1);
  148. }
  149. }
  150. end3: fclose(fp1);
  151.  
  152. getch();
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement