Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. File Processing
  5. File Access
  6. Reading data from random access file

Reading data from random access file

#include <stdio.h>
// clientData structure definition
struct clientData {
  unsigned int acctNum;  // account number
  char lastName[15];     // account last name
  char firstName[10];    // account first name
  double balance;        // account balance
};                       // end structure clientData

int main() 
{
		FILE *cptr;
		int result;
		if((cptr = fopen("credit.dat","rb")) == NULL)
		{
			puts("File could not opened");
		}
		else
		{
			struct clientData client={0,"","",0.0};
			printf("%-6s%-16s%-11s%10s \n","Acct","Last name","First Name","Balance");
			//fread(&client,sizeof(struct clientData),1,cptr);
			while(!feof(cptr))
			{
				
				fread(&client,sizeof(struct clientData),1,cptr);
				if ( client.acctNum != 0)
				{
					printf("%-6d%-16s%-11s%10f\n",client.acctNum,client.lastName,client.firstName,client.balance);
				}
				
			}	
		fclose(cptr);
	}
}  // end main

Output

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments