Make Your Search Easy ! :) Use me

Showing posts with label Data File Handling. Show all posts
Showing posts with label Data File Handling. Show all posts

Friday, February 17, 2017

Writing in a data file :-

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>

class student
 {
  int roll;
  char name[15];
      public:
  void get()
      {
     cout<<"\n enter roll";
     cin>>roll;
     cout<<"\n enter name";
     gets(name);
      }
  void show()
      {
     cout<<"\nRoll :"<<roll<<endl;
     cout<<"\n Name :"<<name<<endl;
       }
  };
void main()
 {
  ofstream fout("stu.dat");
  student s;
  int i;
  for(i=0;i<5;i++)
     {
    s.get();
    fout.write((char*)&s,sizeof s);
     }
 fout.close();
 ifstream fin("stu.dat");
 clrscr();
 cout<<"Details Entered into File are :\n";
 cout<<"------------------------------------------------\n\n";
        while(fin)
     {
          fin.read((char*)&s,sizeof s);
          s.show();
 }
  getch();
 }

To count number of vowels in a file :-

#include<iostream>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>

void main()
 {
  ifstream fin("story.txt");
  char c;
  int count1=0;
  while(fin)
     {
    fin.get(c);
    if(isupper(c))
             {
                c=toupper(c);
               }
    if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
             {
      count1++;
               }
         }
  fin.close();
  cout<<" \nnumber of vowels is     :"<<count1<<endl;
  getch();
 }

Searching a record in a data file :-

#include<iostream.h>
#include<conio.h>
#include<fstream.h>


class student
 {
  int roll;
  char name[15];
      public:
  void show()
      {
     cout<<"\nRoll :"<<roll<<endl;
     cout<<"\n Name :"<<name<<endl;
            }
      int ret_roll()
       {
          return(roll);
         }

  };
void main()
 {

  student s,x;
      int r,flag=0;
      cout<<"Enter the Roll No. to Perform Search: \n";
      cin>>r;
  ifstream fin("stu.dat");
      while(fin)
      {
          fin.read((char*)&s,sizeof s);
            if(s.ret_roll()==r)
             {
                x=s;
                  flag++;
                  break;
               }

   }
      if(flag!=0)
       {
          cout<<"Queried Record is as Follows :\n";
    cout<<"------------------------------------------------\n\n";
            x.show();
         }
      else
          cout<<"Queried Record does Not Exist. ";
  getch();
 }

Counting number of occurrences of a word ("is") in a file :-

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>

void main()
 {
    ifstream fin("msg.txt");
      char word[5];
      int count=0;
      cout<<"CONTENTS OF FILE IS :- \n";
      while(fin)
       {
            fin>>word;
          cout<<word<<" ";
          if(!strcmpi(word,"is"))
             {
                count++;
               }
         }
      cout<<"\nNumber of appearance of \"is\" = "<<count;
      getch();
   }

To modify a record stored in a file :-

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>

class student
   {
  int roll;
  char name[20];
      public:
      void get_roll()
      {
     cout<<"\n enter new roll";
     cin>>roll;
      }
      void get_name()
       {
          cout<<"\n enter new name";
            gets(name);
         }

  void show()
   {
    cout<<"\nRoll :"<<roll<<endl;
    cout<<"\nName :"<<name<<endl;
   }
      int ret_roll()
       {
          return(roll);
         }
 };

void main()
   {
  student s;
  fstream fin("stu.dat",ios::in|ios::out);
  int ch,count=0,pos,r;
  cout<<"\nenter roll to modify : ";
  cin>>r;
  while(fin)
     {
    fin.read((char*)&s,sizeof s);
    if(s.ret_roll()==r)
       {
      cout<<"\ncurrent record is :-\n" ;
      s.show();
      cout<<"\npress 1 to edit name : ";
                  cout<<"\npress 2 to edit roll : ";
      cin>>ch;
      break;
       }
   }
  if(ch==1)
       {
    s.get_name();
   }
  else if(ch==2)
   {
    s.get_roll();
   }
  fin.seekg(-1*sizeof(s),ios::cur);
  fin.write((char*)&s,sizeof s);
  cout<<"\nrecord modified sucessfully\n";
      fin.seekg(-1*sizeof(s),ios::cur);
      fin.read((char*)&s,sizeof s);
      s.show();
  fin.close();
  getch();
 }

To store student detail in file and to display the same :-

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>

class student
 {
    int sno;
      char name[25],stream[10];
      public:
      void get()
       {
          cout<<"\nEnter Student Number : ";
            cin>>sno;
            cout<<"\nEnter Student Name : ";
            gets(name);
            cout<<"\nEnter Student's Stream : ";
            gets(stream);
         }
      void show()
       {
          cout<<"\nStudent Number : "<<sno;
            cout<<"\nStudent Name  : "<<name;
            cout<<"\nStudent's Stream : "<<stream;
         }
   };

void main()
 {
    fstream file("stud.txt",ios::in|ios::out|ios::ate);
    student s;
      int c;
      while(1)
       {
          cout<<"\n\nPress 1 - to enter student detail\nPress 2 - to view all student detail\nPress 4 - to exit :\n";
            cin>>c;
          if(c==1)
             {
                   s.get();
                     file.write((char*)&s,sizeof(s));                     
               }
            else if(c==2)
             {
                 file.seekg(ios::beg);
                   while(!(file.eof()))
                      {
                         file.read((char*)&s,sizeof(s));
                     s.show();
                        }
               }
            else if(c==4)
             {
                   exit(0);
               }
            else
             {
                    cout<<"\nINVALID OPTION !!\n choose correct option from menu......";
               }
         }
   }