Sunday 9 September 2012

File Structure for Direct Access using C++

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
/*  class student
    {
      public:char name[10],rollno[10],marks[10],attendence[10];
         int length;
        // char len[9];
          void getdata()
           {
           cout<<"enter student name:"<<endl;
           gets(name);
           cout<<"enter student rollno:"<<endl;
           cin>>rollno;
           cout<<"enter student marks:"<<endl;
           cin>>marks;
           cout<<"enter student attendence:"<<endl;
           cin>>attendence;
           length=strlen(name)+strlen(rollno)+strlen(marks)+strlen(attendence)+4;

          }
        };
      int main()
        {
          int a[10],n,m,k,j=2;
          char ch,len[10];
          clrscr();
          student s;
          fstream f,f1;
          f.open("direct.ace",ios::in|ios::out|ios::trunc);
          cout<<"enter how many records do u want:"<<endl;
          cin>>n;
          for(int i=0;i<n;i++)
        {
          s.getdata();
          f<<s.length;
          f<<s.name<<"|";
          f<<s.rollno<<"|";
          f<<s.marks<<"|";
          f<<s.attendence<<"#";
          len[i]=s.length;
          a[0]=0;
          a[1]=0;
          a[j]=f.tellp();
          j++;
        }
        for(i=0;i<=n;i++)
        {
          f.open("index.rec",ios::in|ios::out);
          f<<s.name<<"\t";
          f<<s.length<<endl;
           }
        f.close();
        f.open("direct.ace",ios::in|ios::out);
        cout<<"enter access record number:"<<endl;
        cin>>m;
        if(m<=n)
         {
           k=a[m];
           f.seekg(k,ios::beg);
           while(k<a[m+1])
             {
               f.get(ch);
               cout<<ch;
               k++;
             }
          }
          else
            {
              cout<<"ur record does not accessible"<<endl;
            }
             f.close();
             getch();
            return 0;
         }       */
       class student
    {
      public:char name[10],marks[10],attendence[10];
         int length,rollno;
         void getdata()
           {
           cout<<"enter student name:"<<endl;
           gets(name);
           cout<<"enter student rollno:"<<endl;
           cin>>rollno;
           cout<<"enter student marks:"<<endl;
           cin>>marks;
           cout<<"enter student attendence:"<<endl;
           cin>>attendence;
           length=strlen(name)+sizeof(rollno)+strlen(marks)+strlen(attendence)+4;

          }
        };
      int main()
        {
          int n,a[20],r[10],temp,k,g;
          clrscr();
          cout<<"enter how many reords do u want:"<<endl;
          cin>>n;
          student s;
          fstream f,f1;
          f.open("datafile.d",ios::in|ios::out|ios::trunc);
          f1.open("index.i",ios::in|ios::out|ios::trunc);
          for(int i=1;i<=n;i++)
        {
          s.getdata();
          r[i]=f.tellp();
         // f.tellp();
          f<<s.rollno<<"|";
          a[i]=s.rollno;
          f<<s.name<<"|";
          f<<s.marks<<"|";
          f<<s.attendence<<"#";
          }
          for(i=1;i<=n;i++)
             {
               for(int j=i+1;j<=n;j++)
             {
               if(a[i]>a[j])
                 {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
                temp=r[i];
                r[i]=r[j];
                r[j]=temp;
                 }
             }
           }
        //  f1<<"key"<<"\t"<<"referance"<<endl;
         for(i=1;i<=n;i++)
           {


          //   f1<<a[i]<<"\t"<<r[i];
             f1<<a[i]<<"\t";
             f1<<r[i]<<endl;
           }
          cout<<"enter ur direct access record roll number:"<<endl;
          cin>>k;
          //while(f1.read((char *)&r[i],sizeof(r[i])))
//         while(f1)
  /*         {
          // int p;
           //if((f1>>s.rollno)==k)
             f1>>s.rollno;
             int p=s.rollno;
             if(k==p)
             {
            f.seekg(r[k],ios::beg);
            int h=r[k];
            while(h<r[k+1])
                {
                  char ch;
                  f.get(ch);
                  cout<<ch;
                  h++;
                 }
              }
          }
         getch();
         return 0;
       } */
      for(i=1;i<=n;i++)
          {
        f1>>a[i];
        int p=a[i];
        if(p==k)
          {
            f.seekg(r[i],ios::beg);
            g=s.length;
            while(g>0)
            {
            char ch;
            f.get(ch);
            cout<<ch;
            g--;
         }        }
        }

       getch();
       return 0;
       }

No comments:

Post a Comment