Programming in C—Programs on Sequential and Random File Access.

Words
200
Reading
1 min
Listen
Play
6y
Hello Everyone

images.jpeg


Image Source

I hope you all are fine and safe inside your homes. This is a weird time ongoing in the whole world and I hope it will get over soon. As during this time, everyone is locked into their homes. I want to share the C programming language with you. I hope you guys like it, so let's happen today's topic.

General Programs

I am going to write a C program which can be used to find the average of numbers stored on a sequential access file.
Program:

#include
void main()
{
 FILE *fp;
 char ch;
 int n;
 clrscr();
 fp=fopen("C:\\TURBOC3\\fseek.txt", "r");
 if(fp==NULL)
 printf("file cannot be opened");
 else
 {
 printf("Enter value of n to read last n characters :");
 scanf("%d",&n);
 fseek(fp,-n,2);
 while((ch=fgetc(fp))!=EOF)
 {
 printf("%c ",ch);
 }
 }
 fclose(fp);
 getch();
}

The output of the program will be:


Enter the Numbers to be stored in File
Enter -1 to stop entering
95
65
78
98
-1

Some of the Numbers in the file is 328
Average of the Numbers in the file is 82.00000

As my last post was about Sequential and Random file access, so the above program is based on sequential access.
Try it by yourself if you want.

Now for random file access, I will write a program to Transaction processing.
Program:

#include
#include
struct account
{
 int number;
 long amount;
 char name[20];
};
void create()
{
 FILE *fptr;
 int i, n;
 struct account acc;
 if ((fptr = fopen("C:\\TURBOC3\\account.bin","wb")) == NULL){
 printf("File Cannot Open!");
 exit(1);
 }
 printf("Enter Total Number of Customers\n");
 scanf("%d",&n);
 for(i= 1; i <= n; i++)
 {
 acc.number=i;
 printf("Enter Name of User %d : ", i);
 scanf("%s",acc.name);
 printf("Enter Initial Amount of User %d : ", i);
 scanf("%ld",&acc.amount);
 fwrite(&acc, sizeof(struct account), 1, fptr);
 }
 fclose(fptr);
}
void view()
{
 FILE *fptr;
 int n;
 struct account acc;
 if ((fptr = fopen("C:\\TURBOC3\\account.bin","rb")) == NULL){
 printf("File Cannot Open!");
 exit(1);
 }
 while(1)
 {
printf("\nEnter Customer Number to be view : -1 to stop \n");
scanf("%d",&n);
if(n==-1)
break;
fseek(fptr,(n-1)*sizeof(struct account),0);
fread(&acc, sizeof(struct account), 1, fptr);
printf("Customer Number: %d\t Customer Name: %s \tBalance: %ld", 
acc.number,acc.name,acc.amount);
 }
 fclose(fptr);
}
void transfer()
{
 int fromno,tono;
 long tamount;
 struct account acc,fromacc,toacc;
 FILE *fptr;
 if ((fptr = fopen("C:\\TURBOC3\\account.bin","rb+")) == NULL){
 printf("File Cannot Open!");
 exit(1);
 }
 printf("\nEnter From Account No:\n");
 scanf("%d",&fromno);
 printf("\nEnter To Account No:\n");
 scanf("%d",&tono);
 printf("\nEnter Amount to be transfer:");
 scanf("%ld",&tamount);
 fseek(fptr,(fromno-1)*sizeof(struct account),0);
 fread(&fromacc, sizeof(struct account), 1, fptr);
 fseek(fptr,(tono-1)*sizeof(struct account),0);
 fread(&toacc, sizeof(struct account), 1, fptr);
 fromacc.amount-=tamount;
 toacc.amount+=tamount;
 fseek(fptr,(fromno-1)*sizeof(struct account),0);
 fwrite(&fromacc, sizeof(struct account), 1, fptr);
 fseek(fptr,(tono-1)*sizeof(struct account),0);
 fwrite(&toacc, sizeof(struct account), 1, fptr); 
 fclose(fptr);
 
}
void main()
{
 int ch;
 clrscr();
 while(1)
 {
 printf("\n______MENU_______\n");
 printf("1.Create Account\n2.View Account Detail \n3.Transfer Amount\n4.Exit");
 printf("\nEnter Your Choice: ");
 scanf("%d",&ch);
 switch(ch)
 {
 case 1:
create();
break;
case 2:
view();
break;
case 3:
transfer();
break;
case 4:
exit(0);
 }
 }
}

The output of the above program will be:

         MENU
1:Create Account
2:View Account Detail
3:Transfer Amount
4:Exit
Enter Your Choice:1
Enter total Number of Customers:3
Enter Name of User 1: Zeeshan
Enter Initial Amount of User 1:75000
Enter Name of user 2: Abdullah
Enter Initial Amount of User 2:70000
To Be ContinuedCheck Yourself...

If you are interested in it, then have a try and see whether if these programs will work of not.

Logo.png


Gif by @doze

Logo.png

Thank you.

I hope you guys liked my post.

Keep Supporting.

STAY TUNED FOR NEXT POST

UPVOTECOMMENTRESTEEM
IF YOULIKEDMY POST

Logo.png

Created by @zord189

Stay Home, Stay Safe

peerzadazeeshan@peerzadazeeshan

PEACE✌️✌️

Programming in C—Programs on Sequential and Random File Access. | Ecency