0% found this document useful (0 votes)
3 views

exp7

Uploaded by

rubasudhap
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

exp7

Uploaded by

rubasudhap
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

EX.

NO: 7 TELEPHONE DIRECTORY- RANDOM ACCESS FILE

AIM:

To implement a C program to create a telephone directory using


random access file.

ALGORITHM:

Step 1: Start the program

Step 2: Create a structure for telephone directory

Step 3: To insert a record in random file create a file pointer and get
necessary details from user.

Step 4: Using SEEK command move file pointer position to the desired
location and perform file write operation

Step 5: To update a record in a random access file create a file pointer


and get necessary details from user.
Step 6: To delete a record get the record number from the user. If it is
matched in the record number in the file then the details are deleted
Step 7: Stop the program

PROGRAM:-
#include "stdio.h"

#include "string.h"

struct dir

char name[20];

char number[10];

};

void insert(FILE *);

void update(FILE *);


void del(FILE *);

void display(FILE *);

void search(FILE *);

int record = 0;

int main(void)

int choice = 0;

FILE *fp = fopen( "data4.dat",


"w+" ); if (fp == NULL ) perror
("Error opening file"); while
(choice != 6)
{
printf("\n1 insert\t 2 update\n");

printf("3 delete\t 4 display\n");

printf("5 search\t 6 Exit\n


Enter choice:"); scanf("%d",
&choice);
switch(choice)

case 1: insert(fp); break;

case 2: update(fp); break;

case 3: del(fp); break;

case 4: display(fp); break;

case 5: search(fp); break;

default: ;

fclose(fp);

return 0;
}

void insert(FILE *fp)

struct dir contact, blank;

//fseek( fp, -sizeof(struct dir),


SEEK_END ); //fread(&blank,
sizeof(struct dir), 1, fp);
printf("Enter individual/company
name: "); scanf("%s",
contact.name);
printf("Enter telephone number: ");
scanf("%s", contact.number);

fwrite(&contact, sizeof(struct
dir), 1, fp); }

void update(FILE *fp)

char name[20], number[10];

int result;

FILE *fptr;

struct dir contact,blank;

fptr=fopen("data4.dat","a+");

printf("Enter name:");

scanf("%s", name);

rewind(fp);

while(!feof(fptr))

result = fread(&contact, sizeof(struct


dir), 1, fptr); if(result != 0 &&
strcmp(name, contact.name) == 0) {
printf("Enter number:");

scanf("%s",number);

strcpy(contact.number,number);

fseek(fptr, -sizeof(struct dir),


SEEK_CUR); fwrite(&contact,
sizeof(struct dir), 1, fptr);
printf("Updated successfully\
n");
fclose(fptr);

return;
}

printf("Record not found\n");

void del(FILE *fp)

char name[20], number[10];

int result, record=0;

struct dir contact, blank = {"", ""};

printf("Enter name:");

scanf("%s", name);

rewind(fp);

while(!feof(fp))

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strcmp(name, contact.name) == 0) {


fseek(fp, record*sizeof(struct dir),
SEEK_SET);
fwrite(&blank, sizeof(struct dir), 1, fp);
printf("%d Deleted successfully\n", record-
1);
return;
}

record++;

printf("not found in %d records\n", record);


}

void display(FILE *fp)

struct dir contact;

int result;

rewind(fp);

printf("\n\n Telephone directory\n");

printf("%20s %10s\n", "Name",


"Number");
printf("*************************
******\n"); while(!feof(fp))
{

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strlen(contact.name) > 0)

printf("%20s %10s\n",contact.name, contact.number); }


printf("*************************
******\n"); }
void search(FILE *fp)

struct dir contact;

int result; char name[20];

rewind(fp);
printf("\nEnter name:");

scanf("%s", name);
while(!feof(fp))

result = fread(&contact, sizeof(struct dir), 1, fp);

if(result != 0 && strcmp(contact.name, name) == 0) {


printf("\n%20s %10s\n",contact.name, contact.number);
return;
}

printf("Record not found\n");

OUTPUT:-

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 4

Telephone directory

Name Number

*******************************

bb 11111

*******************************

1 insert 2 update
3 delete 4 display

5 search 6 Exit

Enter choice: 5

Enter name: bb
bb 11111

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 1

Enter
individual/company
name:

aa Enter telephone
number:

222222

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 2

Enter name: aa

Enter number: 333333

Updated successfully

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 4
Telephone directory

Name Number

**********************
********* bb 11111
aa 333333
*******************************

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 3

Enter name: aa

1 Deleted successfully

1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 4

Telephone directory

Name Number

**********************
********* bb 11111
*******************************
1 insert 2 update

3 delete 4 display

5 search 6 Exit

Enter choice: 6

RESULT:
Thus the C program on telephone directory was executed successfully.

You might also like