[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

Storing data in a file and displaying it.

Hi,
I want to store data in a file using the following program named insert.c. The corresponding file where the data is stored is credit.dat. I display the content of credit.dat by running the file view.c. the programs are written below. The output while the program view.c is run looks like this:

Acct  Last Name       First name    balance
22    Desi            Baba           555.00
29    Brown           Nancy           23.00
33    Dunn            stacy           78.00
37    Barkar          Doug            77.00
37    Barkar          Doug            77.00


The last record is displayed twice. I want to get rid of the last extra record. Please help.
Thanks,
Sunanda

/* insert.c */
#include <stdio.h>

struct clientData {
      int acctNum;
      char lastName[15];
      char firstName[10];
      float balance;
      };
      
main()
{
FILE *cfPtr;
struct clientData client;

if ((cfPtr = fopen("credit.dat", "r+")) == NULL)
    printf("File could not be opened.\n");
else {
   printf("Enter account number");
   scanf("%d", &client.acctNum);
   while (client.acctNum != 0) {
            printf("Enter lastname\n? ");
      scanf("%s", &client.lastName);
      
      printf("Enter firstname\n? ");
      scanf("%s", &client.firstName);
      
      printf("Enter balance\n? ");
      scanf("%f", &client.balance);
      
      fseek(cfPtr, (client.acctNum - 1) * sizeof(struct clientData),
      SEEK_SET);
   fwrite(&client, sizeof(struct clientData), 1, cfPtr);
   printf("Enter account number\n? ");
   scanf("%d", &client.acctNum);
      }
}
   fclose(cfPtr);
   
   return 0;
   
}       
      

/* view.c */
#include <stdio.h>

struct clientData {
      int acctNum;
      char lastName[15];
      char firstName[10];
      float balance;
      };
      
main()
{
FILE *cfPtr;
struct clientData client;

if ((cfPtr = fopen("credit.dat", "r")) == NULL)
    printf("File could not be opened.\n");
else {
   printf("%-6s%-16s%-11s%10s\n", "Acct", "Last Name", "First name", "balance");

   while (!feof(cfPtr)) {
      fread(&client, sizeof(struct clientData), 1, cfPtr);
     
      if (client.acctNum != 0)
      printf("%-6d%-16s%-11s%10.2f\n", client.acctNum, client.lastName, client.firstName, client.balance);
      }
      }
      fclose(cfPtr);
     
      return 0;
   }
   

0
skundu
Asked:
skundu
1 Solution
 
onkiCommented:
Problem happens in this line:
ยก@while (!feof(cfPtr)) {

as you haven't set the EOF for the data file, the file pointer keep pointing at the last record after the last record is printed once. Afterwards, the while loop runs again and then default finding that it is EOF. So, the last record will be printed twice.

You better set "\0" after writting the last record so as to notify EOF.

0
 
skunduAuthor Commented:
Hi,
Please tell me how to set the EOF. If possible pls write the code for it. The present program is running without problem except that the last record is repeated.
Thanks,
Sunanda


0
 
ozoCommented:
while( fread(&client, sizeof(struct clientData), 1, cfPtr) ){
   if( client.acctNum != 0 ){
         printf("%-6d%-16s%-11s%10.2f\n", client.acctNum, client.lastName, client.firstName, client.balance);
   }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
duneramCommented:
you should use memset to set the 'client' to all 0 before you do the fread.  Also you should check the return from the fread.   If it returns 0 then you know there is no more data to process.  I suspect that when it reads the last line it loads, but hasn't reached EOF yet.  it goes past your top of while loop and then does another read.  but since you didn't 0 set the data and you don't check to see if it really reads, it fails the fread (silently) and displays the last data that was read before the eof was encountered.  Then you hit top of while loop and exit out.

a simple feof check after the read, or a check to see if read returned zero would fix the problem of repeating the last record twice.
0
 
duneramCommented:
while (!feof(cfPtr))
{
          client.acctNum =0; /* set this to 0 so you won't display the last line twice */

          if (fread(&client, sizeof(struct clientData), 1, cfPtr) == 0)
          {
               break; /* no more data to read */
          }
          if (feof(cfPtr))
          {
                break; /* get out of the while loop */
          }
          if (client.acctNum != 0)
          printf("%-6d%-16s%-11s%10.2f\n", client.acctNum, client.lastName, client.firstName, client.balance);
 }
0
 
skunduAuthor Commented:
Hi all,
Thanks you all for responding. The last code given by duneram worked perfectly.
Thanks a lot.
Sunanda
0
 
manjuceeCommented:
Hello skundu,
             It would be better you open the file in binary mode and do all these stuffs. Because in text mode more memory will be occupied.if you wanna store 234 (integer) then it will take 3bytes rather than 2bytes as in case of binary mode.While displaying the records
while(fread(&structvarname,sizeof(struct),1,fileptr)==1)
printf()
will solve your problem.Just try this and reply to me
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now