#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 200
struct emp
{
int id;
char *name;
int eng;
int math;
int hindi;
}*emp1, *emp3;
void display();
void create();
FILE *fp, *fp1;
int count = 0;
void main()
{
int i, n, ch;
printf("1 Create a Record\n");
printf("2 Display Records\n");
printf("3 Exit");
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
fp = fopen("file.txt", "a");
create();
break;
case 2:
fp1 = fopen("file.txt","rb");
display();
break;
case 3:
exit(0);
}
}
}
void create()
{
int i;
char *p;
emp1 = (struct emp *)malloc(sizeof(struct emp));
emp1->name = (char *)malloc((size)*(sizeof(char)));
printf("Enter name of employee : ");
scanf(" %[^\n]s", emp1->name);
printf("Enter emp id : ");
scanf(" %d", &emp1->id);
printf("\nEnter English Marks");
scanf("%d",&emp1->eng);
printf("\nEnter Maths Marks");
scanf("%d",&emp1->math);
printf("\nEnter Hindi Marks");
scanf("%d",&emp1->hindi);
fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
fwrite(emp1->name, size, 1, fp);
fwrite(&emp1->eng, sizeof(emp1->eng), 1, fp);
fwrite(&emp1->math, sizeof(emp1->math), 1, fp);
fwrite(&emp1->hindi, sizeof(emp1->hindi), 1,fp);
count++;
fclose(fp);
}
void display()
{
int i=1;
emp3=(struct emp *)malloc(1*sizeof(struct emp));
emp3->name=(char *)malloc(size*sizeof(char));
if (fp1 == NULL)
printf("\nFile not opened for reading");
printf("Employee ID Employee Name English Maths Hindi");
while (i <= count)
{
fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name, size, 1, fp1);
fread(&emp3->eng, sizeof(emp3->eng), 1, fp1);
fread(&emp3->math, sizeof(emp3->math), 1, fp1);
fread(&emp3->hindi, sizeof(emp3->hindi), 1, fp1);
printf("\n%d %s %d %d %d",emp3->id,emp3->name,emp3->eng,emp3->math,emp3->hindi);
i++;
}
fclose(fp1);
free(emp3->name);
free(emp3);
}
output
1 Create a record
2 Display Records
3 Exit
Enter your choice 1
Enter name of employee: sasas
eneter emp id: 1
Enter Englist Marks 70
Enter Maths Marks 75
Enter Hindi Marks 60
Enter your choice 1
Enter name of employee sasa
enter emp id: 2
Enter English Marks 65
Enter Maths Marks 82
Enter Hindi Marks 70
Enter your choice 2
Employee ID Employee Name English Maths Hindi
1 sasas 70 75 60
2 sasa 65 82 70
#include <stdlib.h>
#include <string.h>
#define size 200
struct emp
{
int id;
char *name;
int eng;
int math;
int hindi;
}*emp1, *emp3;
void display();
void create();
FILE *fp, *fp1;
int count = 0;
void main()
{
int i, n, ch;
printf("1 Create a Record\n");
printf("2 Display Records\n");
printf("3 Exit");
while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
fp = fopen("file.txt", "a");
create();
break;
case 2:
fp1 = fopen("file.txt","rb");
display();
break;
case 3:
exit(0);
}
}
}
void create()
{
int i;
char *p;
emp1 = (struct emp *)malloc(sizeof(struct emp));
emp1->name = (char *)malloc((size)*(sizeof(char)));
printf("Enter name of employee : ");
scanf(" %[^\n]s", emp1->name);
printf("Enter emp id : ");
scanf(" %d", &emp1->id);
printf("\nEnter English Marks");
scanf("%d",&emp1->eng);
printf("\nEnter Maths Marks");
scanf("%d",&emp1->math);
printf("\nEnter Hindi Marks");
scanf("%d",&emp1->hindi);
fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
fwrite(emp1->name, size, 1, fp);
fwrite(&emp1->eng, sizeof(emp1->eng), 1, fp);
fwrite(&emp1->math, sizeof(emp1->math), 1, fp);
fwrite(&emp1->hindi, sizeof(emp1->hindi), 1,fp);
count++;
fclose(fp);
}
void display()
{
int i=1;
emp3=(struct emp *)malloc(1*sizeof(struct emp));
emp3->name=(char *)malloc(size*sizeof(char));
if (fp1 == NULL)
printf("\nFile not opened for reading");
printf("Employee ID Employee Name English Maths Hindi");
while (i <= count)
{
fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name, size, 1, fp1);
fread(&emp3->eng, sizeof(emp3->eng), 1, fp1);
fread(&emp3->math, sizeof(emp3->math), 1, fp1);
fread(&emp3->hindi, sizeof(emp3->hindi), 1, fp1);
printf("\n%d %s %d %d %d",emp3->id,emp3->name,emp3->eng,emp3->math,emp3->hindi);
i++;
}
fclose(fp1);
free(emp3->name);
free(emp3);
}
output
1 Create a record
2 Display Records
3 Exit
Enter your choice 1
Enter name of employee: sasas
eneter emp id: 1
Enter Englist Marks 70
Enter Maths Marks 75
Enter Hindi Marks 60
Enter your choice 1
Enter name of employee sasa
enter emp id: 2
Enter English Marks 65
Enter Maths Marks 82
Enter Hindi Marks 70
Enter your choice 2
Employee ID Employee Name English Maths Hindi
1 sasas 70 75 60
2 sasa 65 82 70
No comments:
Post a Comment