C Program to show an example of Saving Student records to a file


 

C Program to show an example of Saving Student records to a file
#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
struct student
{
        char name[20];
        int regno,s1,s2,s3;
        float total,avg;
};
struct student var,temp;
int fileSize()
{
        int length;
        int des;
        des=open("stud.txt",O_RDONLY);
        length=lseek(des,0,SEEK_END);
        return length;
}
int getNoOfRecords()
{
        return(fileSize()/(sizeof(struct student)));
}
void addStudentRecord()
{
        int f,fp=open("stud.txt",O_CREAT|O_RDWR|O_APPEND);
        printf("Enter the name:");
        scanf("%s",temp.name);
        printf("Enter the reg number:");
        scanf("%d",&temp.regno);
        printf("Enter the marks in s1:");
        scanf("%d",&temp.s1);
        printf("Enter the marks in s2:");
        scanf("%d",&temp.s2);
        printf("Enter the marks in s3:");
        scanf("%d",&temp.s3);
        temp.total=temp.s1+temp.s2+temp.s3;
 temp.avg=temp.total/3;
        int t=0;
        if(f=open("stud.txt",O_RDONLY)!=(-1))
        {
                int counter=0;
                int records=getNoOfRecords();
                while(counter!=records)
                {
                        read(f,&var,sizeof(var));
                        if(var.regno==temp.regno)
                                t=1;
                        counter++;
                }
        }
        if(t!=1)
        {
                write(fp,&temp,sizeof(temp));
                printf("Record inserted into the file...\n");
        }
        else
                printf("Error !!! Record already exists...\n");
        close(fp);
}
void print()
{
        int fp=open("stud.txt",O_RDONLY);
        if(fp!=(-1))
        {
                int counter=0;
                int records=getNoOfRecords();
                printf("Name\tRegNo\tSub1\tSub2\tSub3\tTotal\tAverage\n");
                while(counter!=records)
                {
                        read(fp,&var,sizeof(var));
                        printf("%s\t%d\t%d\t%d\t%d\t%.2f\t%.2f\n",var.name,var.regno,var.s1,var.s2,var.s3,var.total,var.avg);
                        counter++;
                }
  printf("\n");
        }
        else
                printf("Records not found... The file does not exists...\n");
        close(fp);
}
void main()
{
        int a=0;
        while(a!=3)
        {
                printf("Select an Option: \n");
                printf("1.Add Student Record\n");
                printf("2.View all Records\n");
                printf("3.Exit\n");
                scanf("%d",&a);
                switch(a)
                {
                        case 1: addStudentRecord();
                                break;
                        case 2: print();
                                break;
                        case 3: exit(0);
                                break;
                        default : printf("Invalid Option... Try again...\n");
                }
        }
}

Post a Comment

Previous Post Next Post