Q.9 Write a simple database program in C which stores personal details of 100 persons such as name, date of birth, address, phone number etc. (AKTU. 2009-10)
Related Questions -
Q. Write a program to create a suitable struture of the 500 employees in an organization regrading their personnel information [Make suitable assumptions but at least four attributes should be in the structure]. Also list the employees belonging to a particular state. (AKTU. 2012 - 13)
Q. Write a program in C create a text file to store the names of the students of a class. Also display the name having the minimum number of characters. (AKTU. 2013-14)
Ans. /* This program creates a simple database based on file management in C. This program stores personal details of 100 people such as Name, Date of
Birth, Address, Phone number etc.*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
/*number of records */
long records = 100;
/*file name*/
char fname[20] = "person_info.txt";
/* function prototypes */
void input_record(void);
void write_record_to_file(void);
void read_and_display_record_from_file(void);
/* structure of a person */
struct person{
char Name[30];
char Date_of_birth[20];
char Address[100];
char Phone[20];
};
/* global variable declarations */
struct person p1, p2;
FILE *fp;
void main()
{
long i;
for( i = 0; i < records; i++)
{
input_record();
write_record_to_file();
}
read_and_display_record_from_file();
}
void input_record()
{
clrscr();
printf("\n Enter your NAME :\n");
gets(p1.Name);
printf("\n Enter your DATE OF BIRTH :\n");
gets(p1.Date_of_birth);
printf("\n Enter your ADDRESS :\n");
gets(p1.Address);
printf("\n Enter your PHONE :\n");
gets(p1.Phone);
printf("\nPress any key to continue.");
getch();
}
void write_record_to_file(void)
{
if((fp = fopen(fname,"a"))!= NULL)
{
fprintf(fp,"%s\n",p1.Name);
fprintf(fp,"%s\n",p1.Date_of_birth);
fprintf(fp,"%s\n",p1.Address);
fprintf(fp,"%s\n",p1.Phone);
fclose(fp);
}
else
{
printf("\nError opening a file");
printf("\nPress any key to exit");
getch();
exit(1);
}
}
void read_and_display_record_from_file(void)
{
if((fp = fopen(fname,"r"))!=NULL)
{
long i;
for(i = 0; i < records; i++)
{
fgets(p2.Name, 30, fp);
fputs("\nNAME :",stdout);
fputs(p2.Name, stdout);
fgets(p2.Date_of_birth, 20, fp);
fputs("\nDATE OF BIRTH :",stdout);
fputs(p2.Date_of_birth, stdout);
fgets(p2.Address,100,fp);
fputs("\nADDRESS :",stdout);
fputs(p2.Address, stdout);
fgets(p2.Phone, 20, fp);
fputs("\nPHONE :",stdout);
fputs(p2.Phone, stdout);
}
fclose(fp);
printf("\nPress any key to continue.");
getch();
}
else
{
printf("\nError opening a file");
printf("\nPress any key to exit");
getch();
exit(1);
}
}