My Cs Notes
My Cs Notes
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
char name[25];
char Class[4];
float marks;
char grade;
}s1;
void main ()
{ clrscr ();
int rn;
char found='n';
ifstream fin("stu.dat",ios::in);
cin>>rn;
while(!fin.eof())
{ fin.read((char *)&s1,sizeof(s1));
if(s1.rollno==rn)
{ cout<<s1.rollno<<",rollno"
<<s1.grade<<"' grade."<<endl;
found='y';
break;
if(found =='n')
fin.close();
getch ();
OUTPUT:-
39- Program to read and write a structure using write() and read()
function using a binary file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
struct customer { char name[50];
float balance;
};
int main ()
{ clrscr ();
customer savic;
strcpy(savic.name,"Tina marshall");
savic.balance=21310.75;
ofstream fout;
fout.open("saving",ios::out|ios::binary);
if(!fout)
getch();
return 1;
fout.write((char*)&savic,sizeof(customer));
fout.close();
ifstream fin;
fin.open("saving",ios::in|ios::binary);
fin.read((char*)&savic,sizeof(customer));
cout<<"\n\n"<<savic.name;
fin.close();
getch();
return 0;
Output:-
40-Program to delete a record form a data file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
class student
{ int roll;
char name[20];
public:
void getdata();
void display();
int getroll()
{ return roll;
};
void student::getdata()
cin>>roll;
gets(name);
void student::display()
puts(name);
void main ()
clrscr ();
student s;
ifstream fin("stu.dat",ios::in);
int rno;
cin>>rno;
while(fin)
{ fin.read((char*)&s,sizeof(s));
if(s.getroll()==rno)
else
out.write((char*)&s,sizeof(s));
fin.close();
out.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
getch();
}
Output-
41- Program to store TOY data and to display all data stored in file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class toy
{ int id_no;
char toy_name[40];
float price;
public:
void getdata()
cin>>id_no;
gets(toy_name);
cout<<" \n price->" ;
cin>>price;
void showdata()
puts(toy_name);
};
void main()
{ toy t;
clrscr();
t.getdata();
ofstream outfile ;
outfile.open("toy_info.dat",ios::out|ios::app|ios::binary);
outfile.write((char*)&t,sizeof(t));
outfile.close();
ifstream infile;
infile.open("toy_info.dat",ios::in|ios::binary);
for(int i=0;!infile.eof();i++)
{ infile.read((char*)&t,sizeof(t));
t.showdata();
cin>>ch;
if(ch!='y')
break;
infile.close();
getch();
}
OUTPUT:-
42- Program to count the no of spaces, uppercase, lowercase from a
file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void main()
{ clrscr();
ifstream fin("book.dat",ios::in);
char ch;
int countL=0,countU=0,space=0;
while(!fin.eof())
{ fin.get(ch);
if(islower(ch))
countL++;
else
if(isupper(ch))
countU++;
else
if(ch==' ')
space++;
}
43- Program for insertion in linked queue
#include<iostream.h>
#include<conio.h>
#include<process.h>
node *next;
} *front,*newptr,*save,*ptr,*rare;
node*create_new_node(int);
void insert_end(node*);
void display(node*);
void main()
{ clrscr();
front=rare=NULL;
int inf;
char ch='y';
while
(ch=='y'||ch=='Y')
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
exit(0);
}
insert_end(newptr);
display(front);
cin>>ch;
getch();
node*create_new_node(int n)
{ ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
void insert_end(node*np)
{ if(front==NULL)
front=rare=np;
else
{ rare->next=np;
rare=np;
void display(node*np)
{ while(np!=NULL)
{cout<<np->info<<"-> " ;
np=np->next;
}
OUTPUT:-
44 -Program to search for a given character inside a string and to print
the point of match using pointers.
#include<iostream.h>
#include<conio.h>
char *match(char,char*);
void main()
{ clrscr();
char string[80],*cp,ch;
cin.getline(string,80);
cin>>ch;
cp=NULL;
cp=match(ch,string);
if(*cp)
{ cout<<"\n";
for(cp;*cp!='\0';cp++)
cout<<*cp;
else
getch();
char*match(char c,char*s)
{ while(c!=*s && *s)
s++;
return s;
OUTPUT:-
45-Program for insertion in array queue;
#include<iostream.h>
#include<conio.h>
#include<process.h>
int queue[size],front=-1,rare=-1;
void main()
{ int item,res;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
cin>>item;
res=insert_in_q(queue,item);
if(res==-1)
exit(0);
display(queue,front,rare);
cin>>ch;
}
{ if(rare==size-1)
return -1;
else
if(rare==-1)
{ front=rare=0;
queue[rare]=ele;
else
{ rare++;
queue[rare]=ele;
return 0;
{ if (front==-1)
return;
for(int i=front;i<rare;i++)
cout<<queue[i]<<"<-";
cout<<queue[rare]<<endl;
}
OUTPUT:-
36-Program to implement dynamically allocated stack containing names
of countries.
#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ char country[20];
node*link;
};
class stack
{ node *top;
public:
stack()
{ top=NULL;
void push();
void pop();
void display();
};
void stack::push()
gets(temp->country);
temp->link=top;
top=temp;
{ if(top==NULL)
cout<<"\nUNDERFLOW";
else
{ node *ptr=top;
top=top->link;
delete ptr;
void stack::display()
{ node *temp=top;
while(temp!=NULL)
temp=temp->link;
void main()
{ clrscr();
stack st;
int ch;
while(1)
{ cout<<"\n\n\t\t MENU";
cout<<"\n 4-EXIT";
cin>>ch;
switch(ch)
{ case 1: st.push();
break;
case 2: st.pop();
break;
case 3: st.display();
break;
case 4: exit(0);
getch();
clrscr();
getch();
}
OUTPUT:-