Coimbatore, , India
Embedded Systems, C Language, Python
0 टिप्पणी करें | 14 लोगो ने देखा है | 03 अप्रैल 18  | Ashok R
Telephone directory using random access file
AIM
To insert, update, delete and append telephone details of an individual or a company into a telephone directory using random access file.

PROGRAM
#include "stdio.h"
#include "string.h"
#include
#include

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( "telephone.dat", "rb+" );
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;
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)
{
printf("Enter number:");
scanf("%s", number);
strcpy(contact.number, number);
fseek(fp, -sizeof(struct dir), SEEK_CUR);
fwrite(&contact, sizeof(struct dir), 1, fp);
printf("Updated successfully\n");
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


    • इस ब्लॉग के लिए सामाजिक शेयर

पोर्फोलिओ और ब्लॉग
Ashok R विभिन्न कंपनियों का अनुसरण करता है, ये कंपनियां और नियोक्ता Ashok के फिर से शुरू देख सकते हैं
सबसे अच्छा नौकरी के अवसर पाने के लिए अपना फिर से शुरू करें अपलोड करें

मुफ्त रजिस्टर करें!
Ashok-R के ब्लॉग / पोर्टफोलियो