Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file processing 2

Status
Not open for further replies.

annaSparkles

Programmer
Nov 14, 2004
2
BB
hi, im required to write a program that processes information in a file called 'info.dat'. i've gotten this program to work, but my problem is with the info.dat file. i had to write another program to enter the data into info.dat, but 2 of the records are giving me trouble, could someone please look at my code and see whats wrong with it:

Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct memberData
{
    int account;
    int pin;
    char name[20];
    char sname[20];
    char phone[8];
    double lbalance;
    double linstal;
    double cbalance;
};//end structure memberData

//prototypes
int menu(void);
void newRecord(FILE *newfile);
void displayFile(FILE *readfile);


int main()
{
    int request;       //user's request
	FILE *myfile;      //data.dat file pointer

	//fopen opens the file; prints error message if file cannot be opened
	if((myfile = fopen("info.dat","a+")) == NULL)
	{
		printf("\t\t File Cannot Be Opened.\n");
	}//end if

	fclose(myfile);    //fclose closes the file

	//fopen opens the file; prints error message if file cannot be opened
    if((myfile = fopen("info.dat","r+")) == NULL)
	{
		printf("\t\t File Cannot Be Opened.\n");
	}/*end if*/
	else
	{
	    /*allow user to specify action*/
		while((request = menu()) != 3)
		{
			switch(request)
			{
			    case 1:
			           newRecord(myfile);
                       getch();
                       break;
			        
			    case 2:
			           displayFile(myfile);
			           getch();
			           break;
			        
			    default:
                       printf("\n\t\t Invalid Entry.\a\n");
                       getch();
                       break;
                       
			}//end switch
			      								
       }//end while

       fclose(myfile);

    }//end else
    
    return 0;
    
}//end main


//allow user to enter request
int menu(void)
{
	int selection;     //variable to store user's request
	system ("cls");    //clears the display screen

	//display available options
	printf("\n\n\t\t*********************************\n"
	         "\t\t** 1 - Create Records.         **\n"
			 "\t\t** 2 - Display Records.        **\n"
			 "\t\t** 3 - Quit Program.           **\n"
			 "\t\t*********************************\n"
			 "\t\t        Enter Request: ");

			 
    //receive user's selection
    scanf("%d",&selection);

    return selection;

}//end menu


//create and store new record
void newRecord(FILE *newfile)
{
	 int idNum, npin;        //variable to store user's selection

	 //create memberData with default information
	 struct memberData member = { 0, 0, "", "", "", 0.0, 0.0, 0.0 };

	 //prompt user to enter record key
	 printf("\n\n\t\tEnter New Identification Number: ");
	 scanf("%d", &idNum);

	 fseek(newfile,(idNum - 1) * sizeof(struct memberData),SEEK_SET);
	 fread(&member, sizeof(struct memberData),1,newfile);

	 //displays error message if record key is already in use
	 if(member.account != 0 )
	 {
		  printf("\n\n\t\t Id No:%d Already Exists.\n\n",member.account);
	 }/*end if*/
	 else
	 {
          //prompt user to enter to enter data
		  printf("\t\tEnter PIN: ");
		  scanf("%d",&npin);
		  member.pin = npin;
		  
		  printf("\t\tEnter First Name: ");
		  scanf("%s",member.name);
		  
		  printf("\t\tEnter Surname: ");
		  scanf("%s",member.sname);
		  
		  printf("\t\tEnter Telephone No: ");
		  scanf("%s",member.phone);
		  
		  printf("\t\tEnter Loan Balance: ");
		  scanf("%lf",&member.lbalance);
		  
		  printf("\t\tEnter Loan Instalment: ");
		  scanf("%lf",&member.linstal);
		  
		  printf("\t\tEnter Cash Balance: ");
		  scanf("%lf",&member.cbalance);
		  
		  member.account = idNum;

		  //moves file pointer to correct record in file
		  fseek(newfile,(member.account - 1) * sizeof(struct memberData),SEEK_SET);
		  
		  //inserts record in file
		  fwrite(&member,sizeof(struct memberData),1,newfile);
	 }//end else

}//end newRecord


//display contents of the file
void displayFile(FILE *readfile)
{
    //reate memberData with default information
    struct memberData member[100] = { 0, 0, "", "", "", 0.0, 0.0, 0.0 };
    int count = 0;        //count entries in file
    
	printf("\n\n%-7s %-5s %-12s %-9s %-7s %-11s %-12s %-6s\n\n","Acct.",
           "PIN","First Name","Surname","Phone","Loan Bal.",
           "Loan Inst.","Cash");
					
	//reposition file pointers at start of file
	rewind(readfile);
    

    /*print all records from the file to the screen*/
	while(!feof(readfile))
	{
		fread(&member[count],sizeof(struct memberData),1,readfile);

		if(member[count].account != 0)
		{
			printf("%-7d %-5d %-12s %-9s %-7s %-11.2f %-12.2f %-6.2f\n",
			member[count].account,member[count].pin,member[count].name,
            member[count].sname,member[count].phone,member[count].lbalance,
            member[count].linstal,member[count].cbalance);
			count++;
		}//end if

	}//end while

}//end displayFile
 
> if((myfile = fopen("info.dat","r+")) == NULL)
You're using fwrite() and fread(), which typically means you're using binary data.
Use binary mode when opening the file, like so
Code:
if((myfile = fopen("info.dat","r+b")) == NULL)

> fwrite(&member,sizeof(struct memberData),1,newfile);
Between writing a file and reading the file, you should do
Code:
fflush(newfile);

> while(!feof(readfile))
feof() is a status, not a prediction. If your file is 10 bytes long, and you read exactly 10 bytes, then feof() will return FALSE.
feof() will only return TRUE after you've attempted (and failed) to read the 11th byte.
Use this instead
Code:
while ( count < 100 &&
        fread(&member[count],sizeof(struct memberData),1,readfile) == 1 ) {
    /* do stuff */
}
This loop
- prevents you overwriting array elements which don't exist (in case your file is large)
- checks that the fread() actually succeeded

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top