and
is there any possibilities to use fwrite instaed of fprintf to store the data into file.
Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- #include<string.h>
- #include<time.h>
- struct student
- {
- int sno;
- char name[50];
- };
- struct payment
- {
- int previous_amount;
- int present_amount;
- long int total_amount;
- };
- void main()
- {
- struct student d[10];
- struct payment p[10];
- int choice,i = 0;
- int num;
- time_t t;
- time(&t);
- FILE *f1,*f2;
- while(1)
- {
- printf("1.Entering student S.no and Name\n");
- printf("2.Edit student details\n");
- printf("3.Payments\n");
- printf("4.Report particula student\n");
- printf("5.Report of all students\n");
- printf("6.Exit\n");
- printf("Enetr your choice\n");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- f1 = fopen("sno_name.txt","a");
- if(f1 == NULL)
- {
- printf("Error! opening of sno_name file\n");
- exit(1);
- }
- static int a = 0;
- //printf("Enter the details of Student %d:\n",a);
- printf("Enter the details of Student :\n");
- printf("serial Number :");
- scanf("%d",&d[a].sno);
- printf("Name :");
- scanf("%s",d[a].name);
- printf("\n");
- fprintf(f1,"%04d\t\t%20s\n",d[a].sno,d[a].name);
- a++;
- fclose(f1);
- break;
- case 2:
- f1 = fopen("sno_name.txt","w+");
- f2 = fopen("payment_info.txt","w+");
- if(f1 == NULL || f2 == NULL)
- {
- printf("Error1 opening of sno_name file in read mode\n");
- exit(1);
- }
- int m = 0;
- printf("Enter the serial number you want to edit name\n");
- scanf("%d",&num);
- for(m = 0; d[m].sno == num; m++)
- {
- printf("The previous name is: %s\n",d[m].name);
- printf("Enter the modification for that name:");
- scanf("%s",d[m].name);
- printf("the previous amount is: %d\n",p[m].previous_amount);
- printf("Enter the modification for previous payment:");
- scanf("%d",&p[m].previous_amount);
- printf("the present amount is: %d\n",p[m].present_amount);
- printf("Enter the modification for present amount:");
- scanf("%d",&p[m].present_amount);
- p[m].total_amount = p[m].previous_amount + p[m].present_amount;
- }
- for(m = 0; a > m; m++)
- {
- fprintf(f1,"%04d\t%20s\n",d[m].sno,d[m].name);
- fprintf(f2,"%04d\t%s\t%06d\t%06d\t%09d\n",d[m].sno,d[m].name,p[m].previous_amount,p[m].present_amount,p[m].total_amount);
- }
- fclose(f1);
- fclose(f2);
- break;
- case 3:
- printf("the value of a is %d\n",a);
- //printf("you are in case3\n");
- f2 = fopen("payment_info.txt","a");
- f1 = fopen("sno_name.txt","r");
- if(f2 == NULL || f1 == NULL)
- {
- printf("Error! opening payment_info file\n");
- exit(1);
- }
- int b;
- int pay_no;
- printf("Enter the serial number for payment\n");
- scanf("%d",&pay_no);
- for(b = 0; a > b; b++)
- {
- if(d[b].sno == pay_no)
- {
- printf("the value of b is %d\n",b);
- time(&t);
- printf("Enter the payment details of student sno is : %d\n",d[b].sno);
- printf("enter the previous payment:");
- scanf("%d",&p[b].previous_amount);
- printf("enter the present payment:");
- scanf("%d",&p[b].present_amount);
- p[b].total_amount = p[b].present_amount + p[b].previous_amount;
- fprintf(f2,"%04d\t%20s\t%06d\t%06d\t%09d\t%s\n",d[b].sno,d[b].name,p[b].previous_amount,p[b].present_amount,p[b].total_amount,ctime(&t));
- break;
- }
- }
- fclose(f1);
- fclose(f2);
- break;
- case 4:
- f1 = fopen("sno_name.txt","r");
- f2 = fopen("payment_info.txt","r");
- if(f1 == NULL)
- {
- printf("Error! in case 4 opening f1 read mode\n");
- exit(1);
- }
- if(f2 == NULL)
- {
- printf("Error! in case 4 opening f2 read mode\n");
- exit(1);
- }
- int q;
- printf("Enter the student serial number for details\n");
- scanf("%d",&q);
- printf("The details of student is:\n");
- for(i = 0; a > i; i++)
- {
- if(d[i].sno == q)
- {
- printf("Serial number is : %d\n",d[i].sno);
- printf("Name is : %s\n",d[i].name);
- printf("Previous amount : %d\n",p[i].previous_amount);
- printf("Present amount : %d\n",p[i].present_amount);
- printf("The total amount : %d\n",p[i].total_amount);
- break;
- }
- }
- fclose(f1);
- fclose(f2);
- break;
- case 5:
- f1 = fopen("sno_name.txt","r");
- f2 = fopen("payment_info.txt","a");
- if(f1 == NULL)
- {
- printf("Error! opening f1 read mode\n");
- exit(1);
- }
- if(f2 == NULL)
- {
- printf("Error! opening f2 read mode\n");
- exit(1);
- }
- int k = 0;
- printf("the details of student is:\n");
- for(k = 0; a > k;k++)
- {
- printf("The student sno is : %d\n",d[k].sno);
- printf("The student name is :%s\n",d[k].name);
- printf("The student previous payment is: %d\n",p[k].previous_amount);
- printf("The student present payment is: %d\n",p[k].present_amount);
- printf("The total amount is :%d\n",p[k].total_amount);
- printf("\n\n\n");
- }
- fclose(f1);
- fclose(f2);
- f2 = fopen("payment_info.txt","a");
- int n = 0,res = 0;
- for(n = 0; a > n;n++)
- res = res + p[n].total_amount;
- printf("The total amount of all students is : %d\n",res);
- fclose(f1);
- fclose(f2);
- break;
- case 6:
- exit(1);
- default :
- printf("Invalid choice\n");
- }
- }
- return;
- }