Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void writeNamesToFile(char []);
  6. void writerollToFile(char []);
  7. void saveDataToStructure();
  8. void takeInputs();
  9. void showAll();
  10.  
  11. FILE *fp;
  12. int strucsize=0;
  13. int x,i;
  14.  
  15.  
  16.  
  17. struct student
  18. {
  19. char names[100];
  20. int roll;
  21. char dept;
  22. double cgpa;
  23. } info[100];
  24.  
  25.  
  26. int main()
  27. {
  28. ///file theke data niye structure a save korbe...program er surutei ei kaj korbe
  29. saveDataToStructure();
  30. ///
  31.  
  32. ///choice nibe j user ki korte chaay...eikhane search aro habi jabi add korba
  33.  
  34.  
  35. while(1)
  36. {
  37. printf("\n\n\nWhat do you want to do?\n");
  38. printf("1. Add Data");
  39. printf("\n2.Show Data");
  40. printf("\n3.Search info By roll\n");
  41. int choice;
  42. scanf("%d",&choice);
  43.  
  44. if(choice==1)
  45. {
  46. fflush(stdin); ///gtoklker porblem taa....jeno gets ekta skip naa kore
  47. takeInputs();
  48. saveDataToStructure();///input niye toh file a rakhlam...oita abr structure a load kore rakhbo
  49. }
  50. else if(choice==2)
  51. {
  52. showAll();
  53. }
  54. else if(choice==3)
  55. {
  56. search();
  57. }
  58. }
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
  65. ///structure er sob element show korbe...bujhar e kotha
  66. void showAll()
  67. {
  68. int i=0;
  69. for(i=0; i<strucsize; i++)
  70. {
  71. printf("-----%d-----",i+1);
  72. printf("\nStudent name: %s",info[i].names);
  73. printf("Number: %d",info[i].roll);
  74. printf("\n\n");
  75. }
  76. }
  77.  
  78.  
  79. ///input niye oita file a save korbe
  80. void takeInputs()
  81. {
  82. printf("Type exit to stop taking data\n");
  83. char names[100];
  84. char num[100];
  85. while(1)
  86. {
  87. printf("Enter names: ");
  88. gets(names);
  89. if(strcmp(names,"exit")==0)
  90. break;
  91. else
  92. {
  93. printf("Enter roll: ");
  94. gets(num);
  95. if(strcmp(names,"exit")==0) ///same check jodi exit likhe r input nibe na
  96. {
  97. break;
  98. }
  99. else
  100. {
  101. ///prottek taar sathe ekta newline add kore save korbo file a
  102. writeNamesToFile(strcat(names,"\n"));
  103. writerollToFile(strcat(num,"\n"));
  104. }
  105. }
  106. }
  107. }
  108.  
  109. void writeNamesToFile(char str[])
  110. {
  111. fp=fopen("names.txt","a+");
  112. fprintf(fp,str);
  113. fclose(fp);
  114. }
  115. void writerollToFile(char str[])
  116. {
  117. fp=fopen("roll.txt","a+");
  118. fprintf(fp,str);
  119. fclose(fp);
  120. }
  121. void writedepttofiles(char str[])
  122. {
  123. fp=fopen("dept.text","a+");
  124. fprintf(fp,str);
  125. fclose(fp);
  126. }
  127. void writeCGPAtofiles(char str[])
  128. {
  129. fp=fopen("cgpa","a+");
  130. fprintf(fp,str);
  131. fclose(fp);
  132. }
  133.  
  134. void saveDataToStructure()
  135.  
  136. {
  137.  
  138. FILE *fproll,*fpnames,*fpdept,*fpcgpa;
  139.  
  140. fproll=fopen("roll.txt","r+");
  141. fpnames=fopen("names.txt","r+");
  142. fpdept=fopen("dept.txt","r+");
  143. fpcgpa=fopen("cgpa","r+");
  144.  
  145. strucsize=0;
  146. char ch;
  147. char numstr[10][100];
  148. int i=0,j=0;
  149. ///roll save
  150. while((ch=fgetc(fproll))!=EOF)
  151. {
  152. numstr[i][j]=ch;
  153. j++;
  154. if(ch=='\n')
  155. {
  156. info[i].roll=atoi(numstr[i]);
  157. i++;
  158. j=0;
  159. strucsize++;
  160. }
  161. }
  162.  
  163. i=0;
  164. j=0;
  165.  
  166. ///name save
  167. while((ch=fgetc(fpnames))!=EOF)
  168. {
  169. info[i].names[j]=ch;
  170. if(ch=='\n')
  171. {
  172. i++;
  173. j=0;
  174. }
  175. }
  176. i=0;
  177. j=0;
  178. /// dept save
  179. while((ch=fgetc(fpdept))!=EOF)
  180. {
  181. info[i].dept[j]=ch;
  182. j++;
  183. if(ch=='\n')
  184. {
  185. i++;
  186. j=0;
  187. }
  188. }
  189. ///cg save
  190. char cgstr[10][100];
  191. while((ch=fgetc(fpcgpa))!=EOF)
  192. {
  193. cgstr[i][j]=ch;
  194. j++;
  195. if(ch=='\n')
  196. {
  197. info[i].roll=atoi(cgstr[i]);
  198. i++;
  199. j=0;
  200. strucsize++;
  201. }
  202. }
  203. fclose(fproll);
  204. fclose(fpnames);
  205. fclose(fpdept);
  206. fclose(fpcgpa);
  207. }
  208.  
  209. void search(char roll)
  210. {
  211. printf("Enter roll to search: \n");
  212. scanf("%d",&x);
  213. for(i=0;i<strucsize;i++)
  214. {
  215. if(x==info[i].roll)
  216. {
  217. printf("%s\t %d\n%s\t%lf",info[i].names,info[i].roll,info[i].dept,info[i].cgpa);
  218. }
  219. }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement