Create a class MyFile containing: - FILE *fp; - Char fn[maxsize]; Write necessary member Functions using operator overloading: 1. + F3=F1+F2 Put contents of F1 and F2 in F3. 2. - -F3 Changes the case of all upper and lower case characters in F3.


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

class MyFile
{
  fstream fp;
  char *fn;

  public:
  void getfilename(char str[])
  {
int l=strlen(str);
fn=new char[l+1];
strcpy(fn,str);
  }
  //-----------------------------------------------------
  void operator+(MyFile &f)
  {
char ch;
MyFile temp;
fp.open(fn,ios::in);
(f.fp).open(f.fn,ios::in);
temp.getfilename("t3.txt");
temp.fp.open(temp.fn,ios::out);
while(fp.eof()==0)
{
 fp.get(ch);
 (temp.fp).put(ch);
}
while((f.fp).eof()==0)
{
 (f.fp).get(ch);
 (temp.fp).put(ch);
}
fp.close();
f.fp.close();
temp.fp.close();
cout<<endl<<"Contents of file t3 are: ";
temp.display();
-temp;
cout<<endl<<"Contents of file t3 after changing case are: ";
temp.display();
  }
  //-----------------------------------------------------
  void display()
  {
char ch;
fp.open(fn,ios::in);
while(fp.eof()==0)
{
 fp.get(ch);
 cout<<ch;
 getch();
}
fp.close();
  }

  void operator-()
  {
char ch;
int count=0;
fp.open(fn,ios::out|ios::in);
while(fp.eof()==0)
{
 count++;
 fp.read((char*)&ch,sizeof(ch));
 if(isalpha(ch))
 {
if(isupper(ch))
 ch=tolower(ch);
else
 ch=toupper(ch);
fp.seekp((count-1)*sizeof(char));
fp.write((char*)&ch,sizeof(ch));
 }
}
fp.close();
  }
  //-----------------------------------------------------
  void Close()
  {
   fp.close();
  }
};//End of class

int main()
{
  char fname[50];
  MyFile f1,f2;
  clrscr();
  cout<<endl<<"Enter File1 name with its path: ";
  cin>>fname;
  f1.getfilename(fname);
  cout<<endl<<"Enter File2 name with its path: ";
  cin>>fname;
  f2.getfilename(fname);
  f1.operator+(f2);/*
  cout<<endl<<"Contents of File1 are: "<<endl;
  f1.display();
  cout<<endl<<"Contents of File2 are: "<<endl;
  f2.display();
  cout<<endl<<"Contents of File3 are: "<<endl;
  f3.display();
  -f3;
  cout<<endl<<"Contents of File3 after case change are: "<<endl;
  f3.display();      */
  f1.Close();
  f2.Close();
//  f3.display();
  getch();
  return 0;
}







1 comment: