Saturday, 26 August 2017

Program to Read and Write multiple objects in a Binary file

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

struct book
{ char name[30];
  char pname[30];
  long copsold;
};

void main()
{ clrscr();
  book b[10];
  fstream abc;
  abc.open("tanuj.dat",ios::binary,ios::noreplace);
  for(int i=0;i<10;i++)
     {
       cout<<"Enter book name ";
       gets(b[i].name);
       cout<<"Enter publisher's name ";
       gets(b[i].pname);
       cout<<"Ente rno. of copies sold ";
       cin>>b[i].copsold;
       abc.write((char*)&b[i],sizeof(b[i]));
     }
abc.close();
  abc.open("tanuj.dat",ios::binary,ios::in);
  for(i=0;i<10;i++)
  { abc.read((char*)&b[i],sizeof(b[i]));
    puts(b[i].name);
    puts(b[i].pname);
    cout<<b[i].copsold;
  }
  abc.close();
  getch();
}

Sunday, 20 August 2017

Program to Calculate no. of words starting with 'a' or 'A' in a File

#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{ clrscr();
  int count=0,wr=0;
  char str[100];
  char word[20];
  ofstream obj;
  obj.open("tanuj",ios::out);                                         // for writing purpose
  cout<<"Enter anything (100 word limit) ";
  gets(str);
  obj<<str;
  obj.close();
  ifstream abc;
  abc.open("tanuj",ios::in);                                             // for reading purpose
  if(!abc)
    { cout<<"File not found";
      exit(0);
    }
  cout<<"The content in it was ";
  puts(str);
  while(!abc.eof())
       { abc.getline(word,20,' ');
if(word[0]=='a'||word[0]=='A')
  { wr++;
    count++;
  }
else wr++;
       }
  cout<<"\n Total no. of words is "<<wr;
  cout<<"\n Total no. of words starting with a or A is "<<count;
  cout<<"\n Total no. of spaces is "<<wr-1;
  abc.close();
  getch();
}

Program to create a File and write data in it .(FILE HANDLING)

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{ clrscr();
  char word;
  ofstream obj;
  obj.open("tanuj.txt");  
  cout<<"Enter name \n";
  do
    { word=getche();
      obj<<word;
    } while(word!=';');
  cout<<"Thankyou for entering data ";
  obj.close();
  getch();
}

Basic Implementation of Array as a Pointer

#include<iostream.h> #include<conio.h> void main() { clrscr();   int arr[5];   cout<<"Enter 5 nos. ";   f...