You are on page 1of 17

NAME : ADITIYA CHAUHAN

CLASS : XII,A

SUBJECT : COMPUTER
SCIENCE

SUBMITTED TO
T :
1. PROGRAM FOR CREATING A TEXT
FILECHARACTER BY CHARACTER TILL SEMICOLON IS
ENTERED BY THE USER

#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
{
clrscr();
Char ch;
Ofstream fout;
fout.open(“textfile.txt”);
do
{
Ch= getch();
fout<<ch;
}
while (ch!=’;’);
fout.close();
getch();
}

THE OUTPUT OF THE PROGRAMME IS

HELLO MY NAME IS GAURAV SINGH KATOCH


2. PROGRAM FOR READING A TEXT FILE WORD BY
WORD WHICH BEGINS WITH AN UPPERCASE LETTER:

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
Void main()
{
clrscr();
Char str[25];
Ifstream fin;
fin.open(“textfile.txt”);
while(!fin.eof())
{
Fin>>str;
If(isupper(str[0]))
Cout<<str<<endl;
}
Fin.close();
Getch();
}

THE OUTPUT OF THE PROGRAMME

HELLO
3. PROGRAM TO READ AND DISPLAY THE NO. OF TIMES
THE WORD “the” OCCURS AS AN INDEPENDENT WORD
IN A TEXT FILE “DATA.TXT”:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
void main()
{
clrscr();
ifstream.fin;
fin.open(“data.txt”);
char str[60];
int count=0;
while(!fin.eof())
{ fin>>str;
if(strcmp(str,”the”)==0)
count++;
} cout<<”\n no. of times “the” present in the file is:”;
cout<<count;
fin.close();
getch();
}

THE OUTPUT OF THE PROGRAMME IS

NO OF TIMES ”THE” PRESENT IN HE FILE IS: 1


4. program to input lines and write them to a text file:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
void main()
{ clrscr();
char str[80];
char choice=’y’;
ofstream fout;
fout.open(“textfile.txt”);
cout<<”\n enter the contents of a txt file:”;
cout<<”\n enter the blank lines to determine:”;
do{ gets(str);
fout<<str<<endl;
cout<<”want to continue(y/n)\n:”;
cin>>choice; }
while((choice==’y’)||(choice==”Y”));
fout.close();
getch();
}

THE OUTPUT OF THE PROGRAMME IS

ENTER THE CONTENTS OF A TEXT FILE


ENTER HE BLANK LINE TO DETERMINE
WELCOME TO C++ ENVIRONMENT
WANT TO CONTINUE (Y/N): N
5. PROGRAM TO CREATE A NEW FILE ”SECOND.TXT”,
WHICH CONTAINS ALL WORDS FROM THE FILE
“DATA.TXT” IN UPPERCASE:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void main()
{ clrscr();
ifstream fin;
fin.open(“data.txt”);
ofstream fout(“second .txt”);
char ch2,ch;
while(!fin.eof());
fout.put(ch2);
}
fin.close();
fout.close();
cout<<”file conversion done \n”);
getch();
}

THE OUTPUT OF THE PROGRAMME IS

FILE CONVERSION DONE


6. program to count and display thetotal number of
lines and number of lines not starting with “a” present
in a txt file”data.txt”:

#include<iostram.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
void main()
{ clrscr();
ifstream fin;
fin.open(“data.txt”);
char line[60];
int count=0,ctr=0;
while(!fin.eof())
{ fin.getline(line,60);
count++;
if(line[0]!=’a’)
ctr++;
} cout<<”\n no. of lines present in the file are:”;
cout<<count;
cout<<”\n no of times not startig with “a”:”;
fin.close();
getch();
}

THE OUTPUT OF THE PROGRAMME IS

NO. OF LINES PRESENT IN THE FILE ARE: 3


NO. OF LINES NOT STARTING WIH A ARE: 2
7. PROGRAM TO INPUT LINES AND WRITE THEM TO A
TXT FILE:

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
void main()
{ clrscr();
char str[80],choice=’y’;
ofstream fout;
fout.open(“textfile.txt”);
cout<<”\n enter the contents of a txt file:”;
cout<<”\n enter a blank line to determine:”;
do{ gets(str);
fout<<str<<endl;
cout<<”want to continue(y/n)\n”;
cin>>choice;
} while((choice==’y’)||(choice==”Y”));
fout.close();
getch();
}

THE OUTPUT OF THE PROGRAMME IS

ENTER THE CONTEXT OF A TXT FILE


ENTER A BLANK LINE TO DETERMINE
WELCOME TO C++ ENVIRONMENT
WANT TO CONTINUE (Y/N): N
8. TO WRITE DATA IN A BINARY FILE:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
struct student { int roll;
char name;
float marks;};
void main()
{ ofstream of;
of.open(“ren.dat”,ios::binary||ios::out);
student s; char ch;
do{ cin>>s.roll;
ges(s.name);
cin>>s.marks;
of.write{((char*)&s),size of (S))}
cin>>ch;
} while(ch==’y’);
of.close();
}
getch(); }

THE OUTPUT OF THE PROGRAMME IS

1
KATTU RAM
24
9. READING FILE FROM A BINARY FILE “REN.DAT”;

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
struct suden { int roll;
char name;
float marks; };
void main()
{
ifstream if;
if.open(“ren.dat”,ios::binary||ios::in);
while(if.read(((char*)&s),size of(s)))
{
cout<<roll;
puts(name);
cout<<marks; }
if.close();
}
getch();
clrscr();
}

THE OUTPUT OF THE PROGRAMME IS

1
KUTTU RAM
24
10. PROGRAM TO SEARCH FOR A RECORD IN BINARY
FILE:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
struct student
{ int roll;
char name();
float marks;
};
void main()
{
ifstream f;
f.open(“data.dat”,ios::binary||ios::in);
if seekg(roll-1.size of (s))
if.read(char*&s)size of (s));
cout<<s.roll;
cout<<s.marks;
puts(s.name);
if.close();
}
getch();
clrscr();
}
11. PROGRAM TO RENAME THE FILE (“TEMP.DAT” ,
”STUDENT . DAT”)

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void delee()
{ fstream fp,fs;
int rno;
fs.open(“suden.dat”,ios::in|ios::binary);
fp.open(“temp.dat”,ios::ou|ios::binary);
cout<<”enter the roll no whose record is to be deleated:”;
cin>>rno;
while(fs.read(char*)&s,size of (s))
{ if(s.retrollno()!=rno)
{ s.readdata();
fp.write((char*)&s,size of (s));
} else
{ cout<<rno<<”found”;
s.display data(); }
} fs.close();
fp.close();
remove(“student.dat”);
x=rename(“temp.dat”,”student.dat”);
if(x==0)
cout<<”unable to rename”);
} getch();
clrscr(); };
12. PROGRAM TO SEARCH RECORDS ON THE BASIS OF
THE NAME OS THE STUDENT:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void search()
{ student s;
ifstream fin(“student.dat”,ios::binary);
int ctr;
char n[20];
cout<<”enter the name to be searched \n”;
gets(n);
while(fin.read((char*)&s,size of (s))
{ if((strcmp(s.retname(),n)==0)
{ s.display data();
ctr++;
}
}
if(ctr==0)
cou<<”no such recird exist:”;
else
cout<<”no. of records found”<<ctr;
fin.close();
}
getch();
clrscr();
}
13. PROGRAM FOR READING A TXT FILE CHARACTER BY
CHARACTER INCLUDING SPACES:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
void main()
{
clrscr();
char str[80];
ifstream fin;
fin.open(“tesfile.x”);
char ch;
while(!fin.eof()))
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
clrscr();
};
14. PROGRAM O READ AND DISPLAY HE NUMBER OF
TIMES HE WORD “THE” OCCURS AS AN INDEPENDENT
WORD IN A TEXT FILE “DATA.TXT”:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
void main()
{
clrscr();
ifstream.fin;
fin.open(“data.txt”);
char str[60];
int count=0;
while(!fin.eof())
{
fin>>str;
if(strcmp(str,”the”)==0)
count++;
}
cout<<”no. of times the present in the file is:”;
cou<<count;
fin.close();
getch();
clrscr();
}
15. TELLING HE CONCEPT OF FUNCTION OVERLOADING

#include<iostream.h>
#include<conio.h>
void inserionsort(inta[],intn);
void main()
{
int list[20],n;
clrscr;
cout<<”enter the no. of elements:”;
cin>>n;
cout<<”enter the elements:”;
for(int i=0;i,n;i++)
cin>>list[i];
insertionsort(list,n);
getch();
} void insertionsort(int list[],int n)
{ int i,j,k,temp;
{ temp=list[i];
j=i-1;
while((emp<list[j])&&(j>+0))
{ list[j+1]=list[j];
j--;
}list[j+1]=temp;
cout<<”after pass:”<<i<<endl;
for(k=0;k,n;k++)
cout<<list[k]<<”\t”; }
}

You might also like