Advertisement
srijal_kc

About my program in C

Mar 17th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 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. char assignment; float sgpa[8];
  23. float cgpa;
  24. }tm;
  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. char assignment;
  131. //float cgpa;
  132. int conf_record(char id[])
  133. fseek(fp,0,SEEK_END);
  134. char name[20];
  135. char password[10];
  136. printf("\t\tEnter username: ");
  137. scanf("%s",name);
  138. printf("\t\tEnter password: ");
  139. scanf("%s",password);
  140. if (strcmp(name, "abc") == 0 && strcmp(password, "abc") == 0)
  141. {
  142. printf("\t\tPermission Granted\n");
  143. while(another=='y'||another=='Y')
  144. {
  145.  
  146. printf("\n\n\t\tEnter Full Name of Student:\t");
  147. fflush(stdin);
  148. fgets(s.name,100,stdin); //fgets takes an extra \n character as input
  149. s.name[strlen(s.name)-1]='\0';
  150.  
  151. printf("\n\n\t\tEnter Department:\t");
  152. fflush(stdin);
  153. fgets(s.branch,50,stdin);
  154. s.branch[strlen(s.branch)-1]='\0';
  155. fread(&tm,sizeof(tm),1,fp);
  156. do{
  157.  
  158. printf("\n\n\t\tEnter Roll number:\t");
  159. fflush(stdin);
  160. scanf("%d",&s.roll);
  161. if(tm.roll==s.roll)
  162. break;
  163.  
  164. }while(fread(&tm,sizeof(tm),1,fp)==1);
  165.  
  166. fwrite(&s,sizeof(s),1,fp);
  167.  
  168. //printf("\n\n\n\t Enter The assignment:\t);
  169. //scanf("%s", &s.assignment);
  170. //fwrite(&s, sizeof(s), 1, fp);
  171.  
  172. printf("\n\n\t\tWant to enter another student info (Y/N)\t");
  173. fflush(stdin);
  174. another=getchar();
  175. }
  176. }
  177. else
  178. {
  179. printf("\t\tWrong passsword!!\n\t\tPress Any Key\n");
  180. exit;
  181. }
  182.  
  183.  
  184. getch();
  185. }
  186.  
  187.  
  188.  
  189.  
  190. //===================DELETING A RECORD FROM LIST ============
  191. FILE * del(FILE * fp)
  192. {
  193. printHead();
  194. printf("\n\t");
  195. printChar('-',64);
  196. printf("\t\tTo delete information please input username and password\n\n");
  197.  
  198. Student s;
  199. int flag=0,tempRoll,siz=sizeof(s);
  200. FILE *ft;
  201.  
  202. if((ft=fopen("temp.txt","wb+"))==NULL)
  203. {
  204. printf("\n\n\t\t\t\\t!!! ERROR !!!\n\t\t");
  205. system("pause");
  206. return fp;
  207. }
  208. char name[20];
  209. char password[10];
  210. printf("\t\tEnter username: ");
  211. scanf("%s",name);
  212. printf("\t\tEnter password: ");
  213. scanf("%s",password);
  214. if (strcmp(name, "abc") == 0 && strcmp(password, "abc") == 0)
  215. {
  216. printf("\t\tPermission granted\n");
  217. printf("\n\n\tEnter Roll number of Student to Delete the Record");
  218. printf("\n\n\t\t\tRoll No. : ");
  219. scanf("%d",&tempRoll);
  220.  
  221. rewind(fp);
  222.  
  223.  
  224. while((fread(&s,siz,1,fp))==1)
  225. {
  226. if(s.roll==tempRoll)
  227. { flag=1;
  228. printf("\n\tRecord Deleted for");
  229. printf("\n\n\t\t%s\n\n\t\t%s\n\n\t\t%d\n\t",s.name,s.branch,s.roll);
  230. continue;
  231. }
  232.  
  233. fwrite(&s,siz,1,ft);
  234. }
  235.  
  236.  
  237. fclose(fp);
  238. fclose(ft);
  239.  
  240. remove("studentInfo.txt");
  241. rename("temp.txt","studentInfo.txt");
  242.  
  243. if((fp=fopen("studentInfo.txt","rb+"))==NULL)
  244. {
  245. printf("ERROR");
  246. return NULL;
  247. }
  248.  
  249. if(flag==0) printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND \n\t");
  250.  
  251. printChar('-',65);
  252. printf("\n\t");
  253. system("pause");
  254. return fp;
  255.  
  256. }
  257. else
  258. {
  259. printf("\t\tWrong passsword!!\n\t\tPress Any Key\n");
  260. exit;
  261. }
  262.  
  263. getch();
  264.  
  265. }
  266.  
  267.  
  268. //===========MODIFY A RECORD ===========================
  269.  
  270. void modify(FILE * fp)
  271. {
  272. printHead();
  273. printf("\n\t");
  274. printChar('-',64);
  275. printf("\t\tTo modify information please input username and password\n\n");
  276. Student s;
  277. int i,flag=0,tempRoll,siz=sizeof(s);
  278. //float cgpa;
  279. char name[20];
  280. char password[10];
  281. printf("\t\tEnter username: ");
  282. scanf("%s",name);
  283. printf("\t\tEnter password: ");
  284. scanf("%s",password);
  285. if (strcmp(name, "abc") == 0 && strcmp(password, "abc") == 0)
  286. {
  287. printf("\t\tPermission granted\n");
  288. printf("\n\n\tEnter Roll Number of Student to MODIFY the Record : ");
  289. scanf("%d",&tempRoll);
  290.  
  291. rewind(fp);
  292.  
  293. while((fread(&s,siz,1,fp))==1)
  294. {
  295. if(s.roll==tempRoll)
  296. {flag=1;
  297. break;
  298. }
  299. }
  300.  
  301. if(flag==1)
  302. {
  303. fseek(fp,-siz,SEEK_CUR);
  304. printf("\n\n\t\tRECORD FOUND");
  305. printf("\n\n\t\tEnter New Data for the Record");
  306.  
  307. printf("\n\n\t\tEnter Full Name of Student\t");
  308. fflush(stdin);
  309. fgets(s.name,100,stdin);
  310. s.name[strlen(s.name)-1]='\0';
  311.  
  312. printf("\n\n\t\tEnter Branch\t");
  313. fflush(stdin);
  314. fgets(s.branch,50,stdin);
  315. s.branch[strlen(s.branch)-1]='\0';
  316.  
  317. printf("\n\n\t\tEnter Roll number \t");
  318. scanf("%d",&s.roll);
  319.  
  320.  
  321.  
  322.  
  323. fwrite(&s,sizeof(s),1,fp);
  324. }
  325.  
  326. else printf("\n\n\t!!!! ERROR !!!! RECORD NOT FOUND");
  327.  
  328. printf("\n\n\t");
  329. system("pause");
  330.  
  331. }
  332. else
  333. {
  334. printf("\t\tWrong passsword!!\n\t\tPress Any Key\n");
  335. exit;
  336. }
  337.  
  338. getch();
  339.  
  340.  
  341. }
  342.  
  343. //====================DISPLAY THE LIST =================
  344. void displayList(FILE * fp)
  345. { printHead();
  346. Student s;
  347. int i,siz=sizeof(s);
  348.  
  349. rewind(fp);
  350.  
  351. while((fread(&s,siz,1,fp))==1)
  352. {
  353. printf("\n\t\tNAME : %s",s.name);
  354. printf("\n\n\t\tBRANCH : %s",s.branch);
  355. printf("\n\n\t\tROLL : %d\n",s.roll );
  356. printChar('-',64);
  357. }
  358. printf("\n\n\n\t");
  359. printChar('*',65);
  360. printf("\n\n\t");
  361. system("pause");
  362. }
  363.  
  364. void searchRecord(FILE *fp)
  365. {printHead();
  366.  
  367. int tempRoll,flag,siz,i;
  368. Student s;
  369. char another='y';
  370.  
  371. siz=sizeof(s);
  372.  
  373. while(another=='y'||another=='Y')
  374. {
  375. printf("\n\n\tEnter Roll Number of Student to search the record : ");
  376. scanf("%d",&tempRoll);
  377.  
  378. rewind(fp);
  379.  
  380. while((fread(&s,siz,1,fp))==1)
  381. {
  382. if(s.roll==tempRoll)
  383. {flag=1;
  384. break;
  385. }
  386. }
  387.  
  388. if(flag==1)
  389. {
  390. printf("\n\t\tNAME : %s",s.name);
  391. printf("\n\n\t\tBRANCH : %s",s.branch);
  392. printf("\n\n\t\tROLL : %d",s.roll);
  393.  
  394. }
  395. else printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND !!!!");
  396.  
  397.  
  398. printf("\n\n\t\tWant to enter another search (Y/N)");
  399. fflush(stdin);
  400. another=getchar();
  401. }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement