Thursday

Write a program to store std-no, name and mark of ‘n’ students in a data file. Display the records in appropriate format reading from the file.

#include<stdio.h>
#include<conio.h>
struct
 {
  int roll;
  char name[25];
  float mark;
  }std;
void main()
{
  int n,i;
  FILE *fp;
  fp=fopen("d:\\cprg\student.txt","w");
  clrscr();
  printf("\n Enter how many records: ");
  scanf("%d",&n);
  printf("enter student number name and marks for %d students",n);
  for(i=0;i<n;i++)
  {
 scanf("%d%s%f",&std.roll,std.name,&std.mark);
 fprintf(fp,"%d\t%s\t%f\n",std.roll,std.name,std.mark);
  }
  fclose(fp);
  fp=fopen("d:\\cprg\student.txt","r");
  printf("\nRoll\tName\tMarks Obtained\n");
 while(fscanf(fp,"%d%s%f",&std.roll,std.name,&std.mark)!=EOF)
 printf("%d\t%s\t%f\n",std.roll,std.name,std.mark);
  fclose(fp);
  getch();
  }

No comments:

Post a Comment

Rahul-Notes