473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

I have a small doubt on fprintf and fwrite.

The above code is working fine,first am entering the three students data after am closing my console,again am open my console entered again three students data , if am reading all students data am got just last entered few students data,why this occurs???????
and
is there any possibilities to use fwrite instaed of fprintf to store the data into file.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. #include<string.h>
  4. #include<time.h>
  5. struct student
  6. {
  7.     int sno;
  8.     char name[50];
  9. };
  10. struct payment
  11. {
  12.     int previous_amount;
  13.     int present_amount;
  14.     long int total_amount;
  15. };
  16. void main()
  17. {
  18.     struct student d[10];
  19.     struct payment p[10];
  20.     int choice,i = 0;
  21.     int num;
  22.     time_t t;
  23.     time(&t);
  24.     FILE *f1,*f2;
  25.     while(1)
  26.     {
  27.         printf("1.Entering student S.no and Name\n");
  28.         printf("2.Edit student details\n");
  29.         printf("3.Payments\n");
  30.         printf("4.Report particula student\n");
  31.         printf("5.Report of all students\n");
  32.         printf("6.Exit\n");
  33.         printf("Enetr your choice\n");
  34.         scanf("%d",&choice);
  35.         switch(choice)
  36.         {
  37.             case 1:
  38.                     f1 = fopen("sno_name.txt","a");
  39.                     if(f1 == NULL)
  40.                     {
  41.                         printf("Error! opening of sno_name file\n");
  42.                         exit(1);
  43.                     }
  44.                     static int a = 0;
  45.                     //printf("Enter the details of Student %d:\n",a);
  46.                     printf("Enter the details of Student :\n");
  47.                     printf("serial Number :");
  48.                     scanf("%d",&d[a].sno);
  49.                     printf("Name :");
  50.                     scanf("%s",d[a].name);
  51.                     printf("\n");
  52.                     fprintf(f1,"%04d\t\t%20s\n",d[a].sno,d[a].name);
  53.                     a++;
  54.                     fclose(f1);
  55.                     break;
  56.             case 2:
  57.                     f1 = fopen("sno_name.txt","w+");
  58.                     f2 = fopen("payment_info.txt","w+");
  59.                     if(f1 == NULL || f2 == NULL)
  60.                     {
  61.                         printf("Error1 opening of sno_name file in read mode\n");
  62.                         exit(1);
  63.                     }
  64.  
  65.                     int m = 0;
  66.                     printf("Enter the serial number you want to edit name\n");
  67.                     scanf("%d",&num);
  68.                     for(m = 0; d[m].sno == num; m++)
  69.                     {
  70.                             printf("The previous name is: %s\n",d[m].name);
  71.                             printf("Enter the modification for that name:");
  72.                             scanf("%s",d[m].name);
  73.                             printf("the previous amount is: %d\n",p[m].previous_amount);
  74.                             printf("Enter the modification for previous payment:");
  75.                             scanf("%d",&p[m].previous_amount);
  76.                             printf("the present amount is: %d\n",p[m].present_amount);
  77.                             printf("Enter the modification for present amount:");
  78.                             scanf("%d",&p[m].present_amount);
  79.                             p[m].total_amount = p[m].previous_amount + p[m].present_amount;
  80.  
  81.                     }
  82.  
  83.                     for(m = 0; a > m; m++)
  84.                     {
  85.  
  86.                         fprintf(f1,"%04d\t%20s\n",d[m].sno,d[m].name);
  87.                         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);    
  88.                     }
  89.                     fclose(f1);
  90.                     fclose(f2);
  91.                     break;
  92.             case 3:
  93.                     printf("the value of a is %d\n",a);
  94.                     //printf("you are in case3\n");
  95.                     f2 = fopen("payment_info.txt","a");
  96.                     f1 = fopen("sno_name.txt","r");
  97.                     if(f2 == NULL || f1 == NULL)
  98.                     {
  99.                         printf("Error! opening payment_info file\n");
  100.                         exit(1);
  101.                     }
  102.  
  103.                     int b;
  104.                     int pay_no;
  105.  
  106.                     printf("Enter the serial number for payment\n");
  107.                     scanf("%d",&pay_no);
  108.  
  109.                     for(b = 0; a > b; b++)
  110.                     {
  111.                         if(d[b].sno == pay_no)
  112.                         {
  113.                             printf("the value of b is %d\n",b);
  114.                             time(&t);
  115.                             printf("Enter the payment details of student sno is : %d\n",d[b].sno);
  116.                             printf("enter the previous payment:");
  117.                             scanf("%d",&p[b].previous_amount);
  118.                             printf("enter the present payment:");
  119.                             scanf("%d",&p[b].present_amount);
  120.                             p[b].total_amount = p[b].present_amount + p[b].previous_amount;
  121.                             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));
  122.                             break;
  123.                         }                                                                                                                                                                                                                                                                          
  124.                     }
  125.                     fclose(f1);
  126.                     fclose(f2);
  127.                     break;
  128.             case 4:
  129.                     f1 = fopen("sno_name.txt","r");
  130.                     f2 = fopen("payment_info.txt","r");
  131.                     if(f1 == NULL)
  132.                     {
  133.                         printf("Error! in case 4 opening f1 read mode\n");
  134.                         exit(1);
  135.                     }
  136.                     if(f2 == NULL)
  137.                     {
  138.                         printf("Error! in case 4 opening f2 read mode\n");
  139.                         exit(1);
  140.                     } 
  141.                     int q;
  142.                     printf("Enter the student serial number for details\n");
  143.                     scanf("%d",&q);
  144.                     printf("The details of student is:\n");
  145.                     for(i = 0; a > i; i++)
  146.                     {
  147.                         if(d[i].sno == q)
  148.                         {
  149.                             printf("Serial number is : %d\n",d[i].sno);
  150.                             printf("Name is          : %s\n",d[i].name);
  151.                             printf("Previous amount  : %d\n",p[i].previous_amount);
  152.                             printf("Present  amount  : %d\n",p[i].present_amount);
  153.                             printf("The total amount : %d\n",p[i].total_amount);
  154.                             break;        
  155.                         }
  156.                     }
  157.                     fclose(f1);
  158.                     fclose(f2);
  159.                     break;
  160.             case 5:
  161.                     f1 = fopen("sno_name.txt","r");
  162.                     f2 = fopen("payment_info.txt","a");
  163.                     if(f1 == NULL)
  164.                     {
  165.                         printf("Error! opening f1 read mode\n");
  166.                         exit(1);
  167.                     }
  168.                     if(f2 == NULL)
  169.                     {
  170.                         printf("Error! opening f2 read mode\n");
  171.                         exit(1);
  172.                     } 
  173.                      int k = 0;
  174.                 printf("the details of student is:\n");
  175.                     for(k = 0; a > k;k++)
  176.                         {
  177.                             printf("The student sno is : %d\n",d[k].sno);
  178.                             printf("The student name is :%s\n",d[k].name);
  179.                             printf("The student previous payment is: %d\n",p[k].previous_amount);
  180.                             printf("The student present payment is: %d\n",p[k].present_amount);
  181.                             printf("The total amount is :%d\n",p[k].total_amount);
  182.                             printf("\n\n\n");
  183.                         }
  184.                     fclose(f1);
  185.                     fclose(f2);
  186.                 f2 = fopen("payment_info.txt","a");
  187.                 int n = 0,res = 0;
  188.                 for(n = 0; a > n;n++)
  189.                 res = res + p[n].total_amount;
  190.                 printf("The total amount of all students is : %d\n",res);
  191.                     fclose(f1);
  192.                     fclose(f2);
  193.                     break;
  194.             case 6:
  195.                     exit(1);
  196.             default :
  197.                     printf("Invalid choice\n");
  198.         }
  199.  
  200.     }
  201.     return;
  202. }
  203.  
  204.  
  205.  
  206.  
Sep 22 '15 #1
2 1172
To read from a file there should be fscanf. That is missing in your program.
Sep 22 '15 #2
can any one explain when do we use fwrite and when do we use fprintf while storing the data into file...
thanks in advance
Sep 23 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: SandyIsCool | last post by:
Hi I used the below code for an image button image1.imageurl="~/images/image1.jpg"; image1.imageurl=Page.ResolveUrl("~/images/image1.jpg"); Both statements work fine..
1
seshu
by: seshu | last post by:
i have a data grid with eight columns now i want all the data in my data grid to center to there cells ie text alighnment in cell to be center to the cell and one more thing is my data grids first...
1
by: charitra | last post by:
main() inspite of being static can call other non static functions.How come?
1
by: GeezerButler | last post by:
According to msdn, this method throws XmlException when "There is a load or parse error in the XML. In this case, the document remains empty." I cannot understand the difference between load...
4
by: ajaygargnsit | last post by:
Hi I am clear about the nuisances about ptrdiff_t. There is just one thing that I need to know : Suppose we need to display the value of an object (say 'x') of ptrdiff_t. Then what format...
9
by: yashu21985 | last post by:
Why won't this code compile? #include <iostream.h> void Print(int i); void Print(float f, int skip = 0); void main() { Print(3.3); } void Print(int i) {
2
by: koti688 | last post by:
I have two files like below. 1st file : mail.pl #!/usr/bin/perl use DBD::mysql; use DBI; use Utils::DBClass; $object = new DBClass( "perltest", "localhost", 3306, "root", "sierra");
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.