Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5. #include<conio.h>
  6.  
  7. #define Student struct Stud
  8.  
  9. void add(FILE * fp); //to add to list
  10. FILE * del(FILE * fp); //to delete from list
  11. void modify(FILE * fp); //to modify a record
  12. void displayList(FILE * fp); //display whole list
  13. void searchRecord(FILE *fp); //find a particular record
  14. void printChar(char ch,int n); //printing a character ch n times
  15. void printHead(); //printing head line for each screen
  16.  
  17. struct Stud
  18. {
  19. char name[100];
  20. char branch[50];
  21. int roll;
  22. float sgpa[8];
  23. float cgpa;
  24. };
  25.  
  26.  
  27. int main()
  28. {
  29. FILE * fp;
  30. Student s;
  31. int option;
  32. char another;
  33.  
  34. if((fp=fopen("studentInfo.txt","rb+"))==NULL)
  35. {
  36. if((fp=fopen("studentInfo.txt","wb+"))==NULL)
  37. {
  38. printf("can't open file");
  39. return 0;
  40. }
  41. }
  42.  
  43. printHead();
  44. printf("\n\n\t\tCREATED BY");
  45. printf("\n\n\t\tSushant Karki");
  46. printf("\n\n\t\tSrijal K.C.");
  47. printf("\n\n\t\tRuxana Maharjan\n");
  48.  
  49. printf("\n\t\tPRESS ANY KEY TO CONTINUE");
  50. getch();
  51.  
  52. while(1)
  53. {
  54. printHead();
  55. printf("\n\t");
  56. printChar('-',64);
  57. printf("\n\n\t\t\t1. ADD Student");
  58. printf("\n\n\t\t\t2. DELETE Student");
  59. printf("\n\n\t\t\t3. MODIFY Student");
  60. printf("\n\n\t\t\t4. DISPLAY Student List");
  61. printf("\n\n\t\t\t5. Search Record");
  62. printf("\n\n\t\t\t0. EXIT");
  63.  
  64. printf("\n\n\t\tEnter Your Option : ");
  65. scanf("%d",&option);
  66.  
  67. switch(option)
  68. {
  69. case 0: return 1;
  70. break;
  71. case 1: add(fp);
  72. break;
  73. case 2: fp=del(fp);
  74. break;
  75. case 3: modify(fp);
  76. break;
  77. case 4: displayList(fp);
  78. break;
  79. case 5: searchRecord(fp);
  80. break;
  81. default: printf("\n\t\tYou Pressed wrong key");
  82. printf("\n\t\tProgram terminated");
  83. getch();
  84. exit(0);
  85.  
  86. }
  87. }
  88. return 1;
  89.  
  90. }
  91.  
  92. //----printing character ch at n times ------
  93.  
  94. void printChar(char ch,int n)
  95. {
  96. while(n--)
  97. {
  98. putchar(ch);
  99. }
  100. }
  101.  
  102. //-----Printing Head Line of the program -----
  103.  
  104. void printHead()
  105. { system("cls");
  106.  
  107. printf("\n\n\t");
  108. printChar('=',16);
  109. printf("[STUDENT] [INFORMATION] [SYSTEM]");
  110. printChar('=',16);
  111. printf("\n");
  112. }
  113.  
  114.  
  115. // ==========ADDING NEW RECORD==========================
  116.  
  117.  
  118. void add(FILE * fp)
  119. {
  120.  
  121.  
  122. printHead();
  123. printf("\n\t");
  124. printChar('-',64);
  125. printf("\t\tTo add information please input username and password\n\n");
  126.  
  127. char another='y';
  128. Student s;
  129. int i;
  130. //float cgpa;
  131.  
  132. fseek(fp,0,SEEK_END);
  133. char name[20];
  134. char password[10];
  135. printf("\t\tEnter username: ");
  136. scanf("%s",name);
  137. printf("\t\tEnter password: ");
  138. scanf("%s",password);
  139. if (strcmp(name, "abc") == 0 && strcmp(password, "abc") == 0)
  140. {
  141. printf("\t\tPermission Granted\n");
  142. while(another=='y'||another=='Y')
  143. {
  144.  
  145. printf("\n\n\t\tEnter Full Name of Student:\t");
  146. fflush(stdin);
  147. fgets(s.name,100,stdin); //fgets takes an extra \n character as input
  148. s.name[strlen(s.name)-1]='\0';
  149.  
  150. printf("\n\n\t\tEnter Department:\t");
  151. fflush(stdin);
  152. fgets(s.branch,50,stdin);
  153. s.branch[strlen(s.branch)-1]='\0';
  154.  
  155. printf("\n\n\t\tEnter Roll number:\t");
  156. scanf("%d",&s.roll);
  157. fwrite(&s,sizeof(s),1,fp);
  158.  
  159. printf("\n\n\t\tWant to enter another student info (Y/N)\t");
  160. fflush(stdin);
  161. another=getchar();
  162. }
  163. }
  164. else
  165. {
  166. printf("\t\tWrong passsword!!\n\t\tPress Any Key\n");
  167. exit;
  168. }
  169.  
  170.  
  171. getch();
  172. }
  173.  
  174.  
  175.  
  176.  
  177. //===================DELETING A RECORD FROM LIST ============
  178. FILE * del(FILE * fp)
  179. {
  180. printHead();
  181. printf("\n\t");
  182. printChar('-',64);
  183. printf("\t\tTo delete information please input username and password\n\n");
  184.  
  185. Student s;
  186. int flag=0,tempRoll,siz=sizeof(s);
  187. FILE *ft;
  188.  
  189. if((ft=fopen("temp.txt","wb+"))==NULL)
  190. {
  191. printf("\n\n\t\t\t\\t!!! ERROR !!!\n\t\t");
  192. system("pause");
  193. return fp;
  194. }
  195. char name[20];
  196. char password[10];
  197. printf("\t\tEnter username: ");
  198. scanf("%s",name);
  199. printf("\t\tEnter password: ");
  200. scanf("%s",password);
  201. if (strcmp(name, "abc") == 0 && strcmp(password, "abc") == 0)
  202. {
  203. printf("\t\tPermission granted\n");
  204. printf("\n\n\tEnter Roll number of Student to Delete the Record");
  205. printf("\n\n\t\t\tRoll No. : ");
  206. scanf("%d",&tempRoll);
  207.  
  208. rewind(fp);
  209.  
  210.  
  211. while((fread(&s,siz,1,fp))==1)
  212. {
  213. if(s.roll==tempRoll)
  214. { flag=1;
  215. printf("\n\tRecord Deleted for");
  216. printf("\n\n\t\t%s\n\n\t\t%s\n\n\t\t%d\n\t",s.name,s.branch,s.roll);
  217. continue;
  218. }
  219.  
  220. fwrite(&s,siz,1,ft);
  221. }
  222.  
  223.  
  224. fclose(fp);
  225. fclose(ft);
  226.  
  227. remove("studentInfo.txt");
  228. rename("temp.txt","studentInfo.txt");
  229.  
  230. if((fp=fopen("studentInfo.txt","rb+"))==NULL)
  231. {
  232. printf("ERROR");
  233. return NULL;
  234. }
  235.  
  236. if(flag==0) printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND \n\t");
  237.  
  238. printChar('-',65);
  239. printf("\n\t");
  240. system("pause");
  241. return fp;
  242.  
  243. }
  244. else
  245. {
  246. printf("\t\tWrong passsword!!\n\t\tPress Any Key\n");
  247. exit;
  248. }
  249.  
  250. getch();
  251.  
  252. }
  253.  
  254.  
  255. //===========MODIFY A RECORD ===========================
  256.  
  257. void modify(FILE * fp)
  258. {
  259. printHead();
  260. printf("\n\t");
  261. printChar('-',64);
  262. printf("\t\tTo modify information please input username and password\n\n");
  263. Student s;
  264. int i,flag=0,tempRoll,siz=sizeof(s);
  265. float cgpa;
  266. char name[20];
  267. char password[10];
  268. printf("\t\tEnter username: ");
  269. scanf("%s",name);
  270. printf("\t\tEnter password: ");
  271. scanf("%s",password);
  272. if (strcmp(name, "abc") == 0 && strcmp(password, "abc") == 0)
  273. {
  274. printf("\t\tPermission granted\n");
  275. printf("\n\n\tEnter Roll Number of Student to MODIFY the Record : ");
  276. scanf("%d",&tempRoll);
  277.  
  278. rewind(fp);
  279.  
  280. while((fread(&s,siz,1,fp))==1)
  281. {
  282. if(s.roll==tempRoll)
  283. {flag=1;
  284. break;
  285. }
  286. }
  287.  
  288. if(flag==1)
  289. {
  290. fseek(fp,-siz,SEEK_CUR);
  291. printf("\n\n\t\tRECORD FOUND");
  292. printf("\n\n\t\tEnter New Data for the Record");
  293.  
  294. printf("\n\n\t\tEnter Full Name of Student\t");
  295. fflush(stdin);
  296. fgets(s.name,100,stdin);
  297. s.name[strlen(s.name)-1]='\0';
  298.  
  299. printf("\n\n\t\tEnter Branch\t");
  300. fflush(stdin);
  301. fgets(s.branch,50,stdin);
  302. s.branch[strlen(s.branch)-1]='\0';
  303.  
  304. printf("\n\n\t\tEnter Roll number \t");
  305. scanf("%d",&s.roll);
  306.  
  307.  
  308.  
  309.  
  310. fwrite(&s,sizeof(s),1,fp);
  311. }
  312.  
  313. else printf("\n\n\t!!!! ERROR !!!! RECORD NOT FOUND");
  314.  
  315. printf("\n\n\t");
  316. system("pause");
  317.  
  318. }
  319. else
  320. {
  321. printf("\t\tWrong passsword!!\n\t\tPress Any Key\n");
  322. exit;
  323. }
  324.  
  325. getch();
  326.  
  327.  
  328. }
  329.  
  330. //====================DISPLAY THE LIST =================
  331. void displayList(FILE * fp)
  332. { printHead();
  333. Student s;
  334. int i,siz=sizeof(s);
  335.  
  336. rewind(fp);
  337.  
  338. while((fread(&s,siz,1,fp))==1)
  339. {
  340. printf("\n\t\tNAME : %s",s.name);
  341. printf("\n\n\t\tBRANCH : %s",s.branch);
  342. printf("\n\n\t\tROLL : %d",s.roll);
  343. //printf("\n\n\tSGPA: ");
  344. }
  345. printf("\n\n\n\t");
  346. printChar('*',65);
  347. printf("\n\n\t");
  348. system("pause");
  349. }
  350.  
  351. void searchRecord(FILE *fp)
  352. {printHead();
  353.  
  354. int tempRoll,flag,siz,i;
  355. Student s;
  356. char another='y';
  357.  
  358. siz=sizeof(s);
  359.  
  360. while(another=='y'||another=='Y')
  361. {
  362. printf("\n\n\tEnter Roll Number of Student to search the record : ");
  363. scanf("%d",&tempRoll);
  364.  
  365. rewind(fp);
  366.  
  367. while((fread(&s,siz,1,fp))==1)
  368. {
  369. if(s.roll==tempRoll)
  370. {flag=1;
  371. break;
  372. }
  373. }
  374.  
  375. if(flag==1)
  376. {
  377. printf("\n\t\tNAME : %s",s.name);
  378. printf("\n\n\t\tBRANCH : %s",s.branch);
  379. printf("\n\n\t\tROLL : %d",s.roll);
  380.  
  381. }
  382. else printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND !!!!");
  383.  
  384.  
  385. printf("\n\n\t\tWant to enter another search (Y/N)");
  386. fflush(stdin);
  387. another=getchar();
  388. }
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement