Solved

Storing data in a file and displaying it.

Posted on 1998-09-07
7
369 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 170
Can case within switch statement specify range of values ? 3 87
What is sub-make ? 2 71
outlook office 365 8 143
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

856 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