Blog Archive

Sunday, August 29, 2010

Program to create a file containing records of students in the form of structure.

/*
Q17. Program to create a file containing records of students in the
form of structure.
*/
#include<conio.h>
#include<stdio.h>
struct STUDENT
{
char name[100];
int roll;
int marks;
};
void printPosition(struct STUDENT *,int);
void save(struct STUDENT *,int);
void load(struct STUDENT *,int*);
void main()
{
struct STUDENT students[60];
int i=0,roll,marks;
char choice='y';
load(students,&i);
clrscr();
do
{
int c;
printf("\nEnter Roll Number.");

scanf("%d",&roll);

for(c=0; c0)
{
continue;
}
printf("\nEnter Marks.");
scanf("%d",&marks);
if(marks>100 || marks<0) roll="roll;" marks="marks;" choice="="'y'" choice="="'Y');" i="0;" marks="">=60)
{
printf("\n%s",pointer[i].name);
}
}
printf("\nStudents With Second Division.");
for(i=0; i<60>50)
{
printf("\n%s",pointer[i].name);
}
}
printf("\nStudents With Third Division.");
for(i=0; i
{
if(pointer[i].marks<50)
{
printf("\n%s",pointer[i].name);
}
}


}

void save(struct STUDENT *pointer, int count)
{
FILE *fp;
int i=0;
fp = fopen("students.dat","wb");
if(fp==NULL)
{
printf("File Opening Error.");
return;
}
for(;i
{
fwrite(&pointer[i],sizeof(struct STUDENT),1,fp);
}
fclose(fp);
}
void load(struct STUDENT *pointer,int *counter)
{
FILE *fp;
int i;
fp=fopen("students.dat","rb");
if(fp==NULL)
{
return;
}
for(i=0; fread(&pointer[i],sizeof(struct STUDENT),1,fp)==1; i++);
*counter=i;
fclose(fp);
}

No comments:

Post a Comment