Subscribe Us

Responsive Advertisement

Advertisement

problem 40 using c and file by ujjal roy

 

problem 40 using c and file

by ujjal roy



//problem 40

#include<stdio.h>

FILE *file;

struct student

{

    int roll;

    char name[100];

    double cgpa;

    char address[200];


};

int student_cnt=0;

struct student st,stt1[1000000];

void add()

{

     system("cls");

     file=fopen("input.txt","ab+");

     rewind(file);

     printf("Enter roll : ");

     scanf("%d",&st.roll);

     printf("Enter name : ");

     scanf("%s",st.name);

     printf("Enter CGPA : ");

     scanf("%lf",&st.cgpa);

     printf("Enter address : ");

     scanf("%s",st.address);

     student_cnt++;

     fseek(file,0,SEEK_END);

     fwrite(&st,sizeof(st),1,file);

     fclose(file);

}

void display()

{

     system("cls");


    printf("     \troll\t\t ");

    printf("\t\tname\t\t");

     printf("\tcgpa\t\t");

      printf("\taddress\n\n");

    file=fopen("input.txt","ab+");


      rewind(file);

    while(fread(&st,sizeof(st),1,file)==1)

    {

        printf("%15d\t\t",st.roll);

        printf(" %20s\t\t",st.name);

        printf(" %10.2lf\t\t",st.cgpa);

        printf(" %15s\n\n",st.address);


    }

    fclose(file);

    printf("\n\n");

}

void insert(int ro)

{

    file=fopen("input.txt","rb+");

    rewind(file);

    while(fread(&st,sizeof(st),1,file)==1)

    {

        if(ro==st.roll)

        {

            printf("Enter your New roll : ");

            scanf("%d",&st.roll);

            printf("Enter name : ");

            scanf("%s",st.name);

            printf("Enter CGPA : ");

            scanf("%lf",&st.cgpa);

            printf("Enter your address : ");

            scanf("%s",st.address);

            fseek(file,ftell(file)-sizeof(st),0);

            fwrite(&st,sizeof(st),1,file);

            fclose(file);

            getch();

            display();

        return;

        }

    }

    printf("Roll not found \n");

}

void deletee(int ro)

{

    int r;

    r=0;

   //struct student st1[10000];

    int flag=0;

     file=fopen("input.txt","rb+");

    rewind(file);

    while(fread(&st,sizeof(st),1,file)==1)

    {

        if(ro==st.roll)

        {

           flag=1;



        }

        else

        {


           stt1[r]=st;

            r++;


        }

    }

    fclose(file);//here reding file is closed

    if(flag==0)

    printf("Roll not found \n");

    else{


        file=fopen("input.txt","w");

        rewind(file);

        fseek(file,0,SEEK_END);

        for(int i=0;i<r;i++)

        {


            fwrite(&stt1[i],sizeof(stt1[i]),1,file);


        }

        fclose(file);

        getch();

        display();

    }


}

void search(int ro)

{

     file=fopen("input.txt","rb+");

    rewind(file);

    while(fread(&st,sizeof(st),1,file)==1)

    {

        if(ro==st.roll)

        {

            printf("Found\n");

            getch();//created by ujjal roy

           printf("Your name : %s\n",st.name);

           printf("Your CGPA : %.2lf\n",st.cgpa);

           printf("Your address : %s\n",st.address);

         return;

        }

    }

    printf("Roll not found \n");


}

void sortt()

{


     int r;

    r=0;

   //struct student st1[10000];

    int flag=0;

     file=fopen("input.txt","rb+");

    rewind(file);

    while(fread(&st,sizeof(st),1,file)==1)

    {

           stt1[r]=st;

            r++;



    }

    fclose(file);//here reding file is closed

    for(int i=0;i<r-1;i++)

    {

        for(int j=0;j<r-i;j++)

        {

            if(stt1[j].roll>stt1[j+1].roll)

            {

                st=stt1[j];

                stt1[j]=stt1[j+1];

                stt1[j+1]=st;

            }

        }

    }


     file=fopen("input.txt","w");

        rewind(file);

        fseek(file,0,SEEK_END);

        for(int i=0;i<r;i++)

        {


            fwrite(&stt1[i],sizeof(stt1[i]),1,file);


        }

        fclose(file);

        getch();

        display();

}

int main()

{

    printf("a.add info \n");

    printf("b.display info\n");

    printf("c.insert new record\n");

    printf("d.delete record\n");

    printf("e.search a record\n");

    printf("f.sort record\n");

    printf("\nEnter choose : ");

    char ch;

    scanf("%c",&ch);

    if(ch=='a'||ch=='A')

    {

        add(student_cnt);



    }

   else if(ch=='B'||ch=='b')

    {

        display();

    }


    else if(ch=='c'||ch=='C')

    {

        printf("Enter your roll : ");

        int roll;

        scanf("%d",&roll);

        insert(roll);

    }

   else if(ch=='d'||ch=='D')

    {

        printf("Enter your roll : ");

        int roll;

        scanf("%d",&roll);

        deletee(roll);

    }

   else if(ch=='e'||ch=='E')

    {

        printf("Enter your roll : ");

        int roll;

        scanf("%d",&roll);

        search(roll);


    }

   else if(ch=='F'||ch=='f')

    {


        sortt();


    }

    else printf("Wrong choose\n");

    return 0;

}


Post a Comment

0 Comments