Sunday 9 September 2012

CPP program to find out length of a record in a file

#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;
          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()
        {
          char ch;
          clrscr();
          student s;
          fstream f;
          f.open("le.rec",ios::in|ios::out|ios::trunc);
          int n;
          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<<"#";
        }
        f.close();
        f.open("le.rec",ios::in);
        cout<<"records are:"<<endl;
        while(f)
        {
          f.get(ch);
          cout<<ch;
        }
        f.close();
        getch();
        return 0;
       }



No comments:

Post a Comment