bai7_nhansu_f

#include"stdio.h"

#include"conio.h"

#include"iostream.h"

#include"iomanip.h"

#include"fstream.h"

#include"process.h"

#include"string.h"

class nhansu

    {

        private:

            char hoten[35];

            int tuoi;

            char donvi[30];

            char que[20];

        public:

            void nhap();

            void xuat();

            friend void docfile(char *tentep,nhansu *ns,int &n);

            friend void bosung(char *tentep);

            friend void sua(char *tentep);

            friend void chen(char *tentep);

            friend void tim(char *tentep);

            friend void xoa(char *tentep);

            friend void xuatfile(char *tentep);

            friend void ghi(char *tentep,nhansu *ns,int n);

        };

void nhansu::nhap()

    {

        cout<<"

Nhap hoten:"; gets(hoten);

        cout<<"

Nhap tuoi:"; cin>>tuoi;

        cout<<"

Nhap donvi:"; gets(donvi);

        cout<<"

Nhap que:"; gets(que);

        };

void nhansu::xuat()

    {

        cout<<"

"<<setw(5)<<hoten<<setw(6)<<tuoi<<setw(5)<<donvi<<setw(5)<<que;

        }

void taofile(char *tentep)

    {

        FILE *f;

        int i,n;

        nhansu tg;

        f=fopen(tentep,"wb");

        cout<<"

Nhap so ban ghi n="; cin>>n;

        for(i=1;i<=n;i++)

        {

            tg.nhap();

            fwrite(&tg,sizeof(nhansu),1,f);

            }

            fclose(f);

    }

void docfile(char *tentep,nhansu *ns,int &n)

    {

        FILE *f;

        int i,d;

        f=fopen(tentep,"rb");

        fseek(f,0,2);

        d=ftell(f);

        n=d/sizeof(nhansu);

        fseek(f,0,0);

        for(i=1;i<=n;i++)

            fread(&ns[n],sizeof(nhansu),1,f);

        fclose(f);

    }

void bosung(char *tentep)

    {

        FILE *f;

        nhansu tg;

        int n;

        f=fopen(tentep,"rb");

        fseek(f,0,0);

        fread(&n,sizeof(int),1,f);

        n++;

        fwrite(&n,sizeof(int),1,f);

        fclose(f);

        f=fopen(tentep,"ab");

        fseek(f,0,2);

        cout<<"

nhap ban ghi moi:

";

        tg.nhap();

        fwrite(&tg,sizeof(nhansu),1,f);

        fclose(f);

        }

void sua(char *tentep)

    {

        FILE *f,*f1;

        char *tentep1;

        int vt,n,i,d;

        nhansu tg,tg1;

        tentep1="trunggian";

        f=fopen(tentep,"rb");

        f1=fopen(tentep1,"wb");

        //dem so ban ghi

        fseek(f,0,2);

        d=ftell(f);

        n=d/sizeof(nhansu);

        fseek(f,0,0);

        cout<<"

Nhap vi tri can sua"; cin>>vt;

        cout<<"

Nhap du lieu can sua";

        tg.nhap();

        //chuyen du lieu tu f sang f1

        for(i=1;i<=n;i++)

          if (i==vt)

             fwrite(&tg,sizeof(nhansu),1,f1);

          else

             {fread(&tg1,sizeof(nhansu),1,f);

              fwrite(&tg1,sizeof(nhansu),1,f1);

             }

        fclose(f);

        fclose(f1);

        remove(tentep);

        rename(tentep1,tentep);

    }

void xoa(char *tentep)

    {

        FILE *f,*f1;

        char *tentep1;

        int vt,n,i,d;

        nhansu tg,tg1;

        tentep1="trunggian";

        f=fopen(tentep,"rb");

        f1=fopen(tentep1,"wb");

        //dem so ban ghi

        fseek(f,0,2);

        d=ftell(f);

        n=d/sizeof(nhansu);

        fseek(f,0,0);

        cout<<"

Nhap vi tri can xoa"; cin>>vt;

        for(i=1;i<=n;i++)

          if (i!=vt)

             {fread(&tg1,sizeof(nhansu),1,f);

              fwrite(&tg1,sizeof(nhansu),1,f1);

             }

        fclose(f);

        fclose(f1);

        remove(tentep);

        rename(tentep1,tentep);

    }

void tim(char *tentep)

    {

        FILE *f;int i,n,d;

        nhansu tg;

        int tuoi1;

        f=fopen(tentep,"rb");

        fseek(f,0,2);

        d=ftell(f);

        n=d/sizeof(nhansu);

        fseek(f,0,0);

        cout<<"

Nhap tuoi can tim:"; cin>>tuoi1;

        for(i=1;i<=n;i++)

           {

           fread(&tg,sizeof(nhansu),1,f);

           if(tg.tuoi==tuoi1)

            tg.xuat();

         // if(tg.tuoi==tuoi1)

         }

        fclose(f);

    }

void xuatfile(char *tentep)

    {

        FILE *f;

        int i,n,d;

        nhansu tg;

        f=fopen(tentep,"rb");

        fseek(f,0,2);

        d=ftell(f);

        n=d/sizeof(nhansu);

        fseek(f,0,0);

        for(i=1;i<=n;i++)

        {

          fread(&tg,sizeof(nhansu),1,f);

          tg.xuat();

        }

        fclose(f);

    }

void ghi(char *tentep,nhansu *ns,int n)

    {

        FILE *f;

        int i;

        f=fopen(tentep,"wb");

        fflush(f);

        for(i=1;i<=n;i++)

        fwrite(&ns[i],sizeof(nhansu),1,f);

        fclose(f);

        }

void main()

    {

        char *tentep;

        nhansu ns[100];

        int i,n;

        clrscr();

        cout<<"

Nhap ten file:"; gets(tentep);

        taofile(tentep);

        cout<<"

Danh sach tu file sau khi nhap:

";

        cout<<"

hoten    tuoi ngay sinh     que:";

        xuatfile(tentep);

        docfile(tentep,ns,n);

        cout<<"

Danh sach nhan su tu lop la:";

        for(i=1;i<=n;i++)

        ns[i].xuat();

        bosung(tentep);

        cout<<"

Danh sach sau khi bo sung la:";

        xuatfile(tentep);

        sua(tentep);

        cout<<"

Danh sach sau khi sua la:";

        xuatfile(tentep);

        tim(tentep);

        xoa(tentep);

        cout<<"

Danh sach sau khi xoa la:";

        xuatfile(tentep);

        //ghi(tentep,ns,n);

        getch();

    }

Bạn đang đọc truyện trên: TruyenTop.Vip

Tags: #123