Advertisement
Guest User

Untitled

a guest
May 25th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. // data in file is like
  2. // No. Name Mark1 Mark2 Mark3
  3.  
  4.  
  5. // Use full file path instead of just file name if any error occurs !!
  6.  
  7. function menu()
  8. disp("1 : Read File");
  9. disp("2 : Write File");
  10. disp("3 : Search in File");
  11. disp("4 : Deletion File");
  12. disp("5 : Copy file");
  13. disp("6 : EXIT");
  14. endfunction
  15.  
  16. function read_file()
  17. fp=mopen('data1.txt','rt');
  18.  
  19. while 1
  20. [scanned_latters,no,name,mark1,mark2,mark3]=mfscanf(fp,"%s %s %d %d %d");
  21.  
  22. if scanned_latters==-1 then // it means end of a file
  23. break;
  24. end
  25.  
  26. avg=(mark1 + mark2 + mark3)/3;
  27.  
  28. mprintf("%s %s %f\n",no,name,avg);
  29. end
  30.  
  31.  
  32.  
  33. mclose(fp);
  34. endfunction
  35.  
  36. function write_file()
  37. fp=mopen('data1.txt','at');
  38.  
  39. want_to_write=%t
  40.  
  41. while want_to_write
  42.  
  43. no=input("No : ","string");
  44. name=input("Name : ","string");
  45. mark1=input("Mark 1 : ");
  46. mark2=input("Mark 2 : ");
  47. mark3=input("Mark 3 : ");
  48.  
  49. mfprintf(fp,"%s %s %d %d %d\n",no,name,mark1,mark2,mark3);
  50.  
  51. x=input("Want to write further (yes : 1) || (No : 0)");
  52.  
  53. if x==0 then
  54. want_to_write=%f;
  55. end
  56. end
  57.  
  58. mclose(fp);
  59. endfunction
  60.  
  61. function search_file()
  62. fp=mopen('data1.txt','rt');
  63.  
  64. x=input("Search by ROLL NO (PRESS 1) || by NAME (PRESS 2)");
  65. search=input("Enter search name | roll no :","string");
  66.  
  67. search_rollno="NULL";
  68. search_name="NULL";
  69.  
  70. if x==1 then
  71. search_rollno=search;
  72. else
  73. search_name=search;
  74. end
  75.  
  76. while 1
  77. [scanned_latters,no,name,mark1,mark2,mark3]=mfscanf(fp,"%s %s %d %d %d");
  78.  
  79.  
  80. if scanned_latters==-1 then // it means end of a file
  81. disp("--- Record Not Found ---");
  82. break;
  83. end
  84.  
  85.  
  86. if search_rollno=="NULL" & search_name==name then // search by name
  87. mprintf(" Record Founded :: %s %s %d %d %d\n",no,name,mark1,mark2,mark3);
  88. break;
  89. end
  90.  
  91. if search_name=="NULL" & search_rollno==no then // search by roll no
  92. mprintf(" Record Founded :: %s %s %d %d %d\n",no,name,mark1,mark2,mark3);
  93. break;
  94. end
  95.  
  96. end
  97.  
  98. mclose(fp);
  99. endfunction
  100.  
  101. function copy_file()
  102.  
  103. x=input("Enter a name of new file : ","string");
  104.  
  105. fp=mopen("data1.txt","rt"); // source file
  106. fp2=mopen(x,"w"); // destination file
  107.  
  108. while 1
  109. [scanned_latters,no,name,mark1,mark2,mark3]=mfscanf(fp,"%s %s %d %d %d");
  110.  
  111. if scanned_latters==-1 then
  112. break;
  113. end
  114.  
  115. mfprintf(fp2,"%s %s %d %d %d\n",no,name,mark1,mark2,mark3);
  116.  
  117. end
  118.  
  119. mclose(fp);
  120. mclose(fp2);
  121. endfunction
  122.  
  123. function delete_file() // delete record from file
  124.  
  125. fp=mopen("data1.txt","rt");
  126. fp2=mopen("temp.txt","wt");
  127.  
  128. x=input("Delete by ROLL NO (PRESS 1) || by NAME (PRESS 2)");
  129. y=input("Enter name | roll no :","string");
  130.  
  131. delete_rollno="NULL";
  132. delete_name="NULL";
  133.  
  134. if x==1 then
  135. delete_rollno=y;
  136. else
  137. delete_name=y;
  138. end
  139.  
  140.  
  141. found=%f; // record found or not ?
  142.  
  143. while 1
  144. [scanned_latters,no,name,mark1,mark2,mark3]=mfscanf(fp,"%s %s %d %d %d");
  145.  
  146.  
  147. if scanned_latters==-1 then // it means end of a file
  148.  
  149. if found==%t then
  150. disp(" :: Record Successfully deleted ::");
  151. else
  152. disp(" :: Record Not found in file ::");
  153. end
  154.  
  155. break;
  156. end
  157.  
  158.  
  159. if delete_rollno=="NULL" & delete_name==name then // search by name
  160. found=%t;
  161. elseif delete_name=="NULL" & delete_rollno==no then
  162. found=%t;
  163. else
  164. mfprintf(fp2,"%s %s %d %d %d\n",no,name,mark1,mark2,mark3);
  165. end
  166.  
  167.  
  168. end
  169.  
  170. mclose(fp);
  171. mclose(fp2);
  172.  
  173. movefile("temp.txt","data1.txt");
  174. mdelete("temp.txt");
  175.  
  176. endfunction
  177.  
  178.  
  179.  
  180. flag=%t; // flag=1 -> continue loop
  181.  
  182. while flag
  183.  
  184. menu(); // shows menu
  185.  
  186. choice=input("Select any option :");
  187.  
  188. select choice
  189. case 1 then
  190. read_file();
  191.  
  192. case 2 then
  193. write_file();
  194.  
  195. case 3 then
  196. search_file();
  197.  
  198. case 4 then
  199. delete_file();
  200.  
  201. case 5 then
  202. copy_file();
  203.  
  204. case 6 then
  205. flag=%f;
  206.  
  207. end
  208. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement