We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Storing data in a file and displaying it.

skundu
skundu asked
on
Medium Priority
445 Views
Last Modified: 2012-05-04
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;
   }
   

Comment
Watch Question

Commented:
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.

Author

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


ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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);
   }
}
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
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);
 }

Author

Commented:
Hi all,
Thanks you all for responding. The last code given by duneram worked perfectly.
Thanks a lot.
Sunanda

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.