Ex - No:10b Sequential Access File AIM
Ex - No:10b Sequential Access File AIM
AIM
To write a C program to count the number of account holders whose balance is less than the min-
imum balance using sequential access file.
ALGORITHM
Step 1: Start the program.
Step 2: Create a Bank structure
Step 3: Read the contents from the user
Step 4: Write it to a file
Step 5: Close the file
Step 6: Open the file in read mode
Step 7: Read the contents. If the balance is less than minimum display
Step 8: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
struct bank
{
char name[10];
int acc;
double bal;
}*s;
int n,i;
int ac;
double min;
printf(“\nEnter the record size:”);
scanf(“%d”,&n);
s=(struct bank*)malloc(n*sizeof(struct bank));
FILE *f;
f=fopen(“bdata.txt”,”w”);
for(i=0;i<n;i++)
{
printf(“\nName:”);
scanf(“%s”,&s[i].name);
printf(“\nAcc no.:”);
scanf(“%d”,&s[i].acc);
printf(“\nBalance:”);
scanf(“%ld”,&s[i].bal);
}
for(i=0;i<n;i++)
{
fprintf(f,”%s %d %ld”,s[i].name,s[i].acc,s[i].bal);
}
fclose(f);
f=fopen(“bdata.txt”,”r”);
printf("\nEnter minimum balance:");
scanf("%ld",&min);
while(!feof(f))
{
for(i=0;i<n;i++)
{
fscanf(f,”%s %d %ld”,&s[i].name,&s[i].acc,&s[i].bal);
if(s[i].bal<min)
printf(“\nName=%s\nAcc. No.=%d\nBalance=
%ld\n”,s[i].name,s[i].acc,s[i].bal);
}
}
fclose(f);
getch();
}
SAMPLE OUTPUT
Name: Abi
Acc No.: 123
Balance: 5000
Name: Hari
Acc No.: 124
Balance: 500000
Name: Sam
Acc No.: 125
Balance: 1000
Name: Pradeep
Acc No.: 126
Balance: 10000
Name: Gokul
Acc No.: 127
Balance: 500
RESULT
Thus the C program to count the number of account holders whose balance is less than
the minimum balance using sequential access file was written, entered, executed and the output
was verified.
AIM
To write a C program to insert, update, delete and append telephone details of an individual or a
company into a telephone directory using random access file.
ALGORITHM
Step 1: Start the program.
Step 2: Create a Structure with Phone Number and Name
Step 3: Open the File
Step 4: Get the contents from the user
Step 5: Get the position randomly
Step 6: Insert it if new or else update
Step 7: Repeat till no input is given
Step 8: Stop the Program
PROGRAM
#include <stdio.h>
struct Telephone
{
unsigned int phno;
char Name[ 15 ];
};
int main( void )
{
FILE *fp;
struct Telephone d = { 0, ""};
if ( ( fp = fopen( "directory.txt", "rb+" ) ) == NULL )
{
puts( "File could not be opened." );
}
else
{
printf( "Enter Phone Number: " );
scanf( "%d", &d.phno );
while ( d.phno != 0 )
{
printf( "Enter Name: " );
fscanf( stdin, "%14s ", d.Name);
fseek( fp, ( d.phno - 1 ) * sizeof( struct Telephone ), SEEK_SET );
fwrite( &d, sizeof( struct Telephone ), 1, phno );
printf( " Enter Phone Number: " );
scanf( "%d", &d.phno );
}
fclose( cfPtr );
}
}
SAMPLE OUTPUT
RESULT
Thus the C program to insert, update, delete and append telephone details of an individual
or a company into a telephone directory using random access file was written, entered, executed
and the output was verified