Saturday, July 30

To Read Student Details and Make Rank List

Here is a simple C program that can be used to input students details. The program can store the details into a file and from that file it can make a rank list which can be written to another file.!! On reading the aim it is obvious that actual file handling may not be in same manner. But this program surely introduce you how Actual file 'read ' and 'write' occurs. Here we go..




STUDENT.C

/*
*PROGRAM NAME:STUDENT.C
*AIM : TO READ STUDENT DETAILS AND MAKE PROGRESS REPORT IN A FILE
*OUTPUT FILE: STUDENT.TXT , PROGRESS. TXT
*DATE: 07/30/2011
*/




#include<stdio.h>
#include<fcntl.h>
struct student
{
       int roll_no,total;
       char Name[10];
        }
       buff,stud[20];
      
       int main()
       {

           int count,i,j;
           int fd=open("STUDENT.TXT",O_WRONLY|O_APPEND);  //open 'student.txt' to write unsorted student-details
           printf("Enter the no. of students..");
           scanf("%d",&count);
           for( i=0;i<count;printf("\n"))
           {
           printf("Student %d\n",++i);
           printf("Roll no : ");
           scanf("%d",&buff.roll_no);
           printf("Name : ");
           scanf("%s",buff.Name);
           printf("Total Marks : ");
           scanf("%d",&buff.total);
           write(fd,&buff,sizeof(buff));  //Store each data in a "buffer" structure and append to 'student.txt'..
           }                                            
            close(fd);
            
             fd=open("STUDENT.TXT",O_RDONLY);
            int fd2=open("PROGRESS.TXT",O_WRONLY);
            count=0;
            while(read(fd,&buff,sizeof(buff))>0)  //Open 'student.txt' in read mode and copy data to structure-array..
                 stud[count++]=buff;
                
               for(i=0;i<count;i++)                //Sort the details in structure array
               for(j=0;j< count-i-1;j++)
               if(stud[j].total<stud[j+1].total)
               {
                buff=stud[j];
                stud[j]=stud[j+1];
                stud[j+1]=buff;
                }
               for(i=0;i<count;i++)
               write(fd2,&stud[i],sizeof(stud[i]));//Write back the sorted details to 'progress.txt'..
               close(fd2);
               close(fd);
               fd2=open("PROGRESS.TXT",O_RDONLY); //Open 'progress.txt' file to read and display ranklist 
               printf("\n\tPROGRESS REPORT\n\t_______________\n\n\n RANK\t NAME \tROLLNO\tMARKS\n\n");
               i=0;
               while(read(fd2,&buff,sizeof(buff))>0)
               printf("\n (%d)\t%s\t%d\t%d",++i,buff.Name,buff.roll_no,buff.total);
               getch();
                           }

0 comments :

Post a Comment