Solved

Storing data in a file and displaying it.

Posted on 1998-09-07
7
363 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;
   }
   

0
Comment
Question by:skundu
7 Comments
 

Expert Comment

by:onki
ID: 1252536
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
 

Author Comment

by:skundu
ID: 1252537
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
 
LVL 84

Expert Comment

by:ozo
ID: 1252538
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Accepted Solution

by:
duneram earned 50 total points
ID: 1252539
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
 
LVL 2

Expert Comment

by:duneram
ID: 1252540
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
 

Author Comment

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

Expert Comment

by:manjucee
ID: 1252542
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now