16. Write a program that uses functions to perform the following operations:
a. To copy contents of one file into another file.
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
fstream ifile;
fstream ofile;
char fname[30],st[30];
ofile.open("temp.cpp",ios::out);
cout<<"\n Enter the existing file name";
cin>>fname;
ifile.open(fname,ios::in);
while(ifile)
{
ifile.getline(st,30);
ofile<<st;
}
ifile.close();
ofile.close();
cout<<"\n The contents of the copied file are\n";
fstream tfile;
tfile.open("temp.cpp",ios::in);
char str[30];
while(tfile)
{
tfile.getline(str,30);
cout<<str;
}
} .To count the no of occurrences of a word in a given file
# include<iostream>
# include<fstream>
# include<string.h>
using namespace std;
int main()
{
char fname[30];
cout<<"\n enter a file";
cin>>fname;
fstream file;
file.open(fname,ios::in);
char key[30],st[30],line[30],ch;
int i,j,c=0;
cout<<"enter the string to be searched for";
cin>>key;
file.get(ch);
//cout<<"the characters read is"<<ch;
j=0;
i=strlen(key);
cout<<"the length of given search string is:"<<i;
while(file.get(ch))
{
//cout<<ch;
if(ch==key[0])
{
j=0;
while(j<i)
{
file.get(ch);
//cout<<ch;
if(ch!=key[++j])
break;
}
if(j==1)
c++;
}
}
cout<<"\nthe no. of occurence of"<<key<<"are"<<c;
file.close();
}
c.To count the no of occurrences of a word in a given file
# include<iostream>
# include<fstream>
# include<string.h>
using namespace std;
int main()
{
char fname[30];
cout<<"\n enter a file";
cin>>fname;
fstream file;
file.open(fname,ios::in);
char key[30],st[30],line[30],ch;
int i,j,c=0;
cout<<"enter the string to be searched for";
cin>>key;
file.get(ch);
//cout<<"the characters read is"<<ch;
j=0;
i=strlen(key);
cout<<"the length of given search string is:"<<i;
while(file.get(ch))
{
//cout<<ch;
if(ch==key[0])
{
j=0;
while(j<i)
{
file.get(ch);
//cout<<ch;
if(ch!=key[++j])
break;
}
if(j==1)
c++;
}
}
cout<<"\nthe no. of occurence of"<<key<<"are"<<c;
file.close();
}
a. To copy contents of one file into another file.
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
fstream ifile;
fstream ofile;
char fname[30],st[30];
ofile.open("temp.cpp",ios::out);
cout<<"\n Enter the existing file name";
cin>>fname;
ifile.open(fname,ios::in);
while(ifile)
{
ifile.getline(st,30);
ofile<<st;
}
ifile.close();
ofile.close();
cout<<"\n The contents of the copied file are\n";
fstream tfile;
tfile.open("temp.cpp",ios::in);
char str[30];
while(tfile)
{
tfile.getline(str,30);
cout<<str;
}
} .To count the no of occurrences of a word in a given file
# include<iostream>
# include<fstream>
# include<string.h>
using namespace std;
int main()
{
char fname[30];
cout<<"\n enter a file";
cin>>fname;
fstream file;
file.open(fname,ios::in);
char key[30],st[30],line[30],ch;
int i,j,c=0;
cout<<"enter the string to be searched for";
cin>>key;
file.get(ch);
//cout<<"the characters read is"<<ch;
j=0;
i=strlen(key);
cout<<"the length of given search string is:"<<i;
while(file.get(ch))
{
//cout<<ch;
if(ch==key[0])
{
j=0;
while(j<i)
{
file.get(ch);
//cout<<ch;
if(ch!=key[++j])
break;
}
if(j==1)
c++;
}
}
cout<<"\nthe no. of occurence of"<<key<<"are"<<c;
file.close();
}
c.To count the no of occurrences of a word in a given file
# include<iostream>
# include<fstream>
# include<string.h>
using namespace std;
int main()
{
char fname[30];
cout<<"\n enter a file";
cin>>fname;
fstream file;
file.open(fname,ios::in);
char key[30],st[30],line[30],ch;
int i,j,c=0;
cout<<"enter the string to be searched for";
cin>>key;
file.get(ch);
//cout<<"the characters read is"<<ch;
j=0;
i=strlen(key);
cout<<"the length of given search string is:"<<i;
while(file.get(ch))
{
//cout<<ch;
if(ch==key[0])
{
j=0;
while(j<i)
{
file.get(ch);
//cout<<ch;
if(ch!=key[++j])
break;
}
if(j==1)
c++;
}
}
cout<<"\nthe no. of occurence of"<<key<<"are"<<c;
file.close();
}
Comments
Post a Comment