Program To File Handling In C++ :








You Can Copy This Code And Past On Your Editor. and you have to save this file with '.cpp' exstension.  





Program :







//1) Create a C++ class My File with data members file pointer and filename. Write necessary member functions to accept and display File. Overload the following operators:
//Operator                Example                  Purpose
// +                          F3 F1 F2                    Concatenate the contents of file F1 and F2 in F3.
//!                                  IF3                   Changes the case of alternate characters of file F3.

Ans:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define MAXSIZE(10)

Class myfile
{
FILE *fp;
Char fn [MAXSIZE];
Public:
Myfile(char*fname)
{
Strcpy(fn,fname);
}
Myfile operator +(myfile);
Void operator !();
Void display();
};
Void myfile:display();
{
fp=fopen(fn,"r");
char ch;
while((ch=fgetc(fp))!=EOF)
{
Cout<<ch;
}
Fclose(fp);
}
Void mylife::operator !()
{
Mylife f4("sy.txt");
Char ch;
Fp=fopen(fn,"r");
F4.fp=fopen(f4.fn,"w");
While((ch=fgetc(fp))!=EOF);
{
If(isupper(ch))
Fputc(tolower(ch),f4.fp);
Else if (islower(ch))
Fputc(toupper(ch),f4.fp);
Else
Fputc(ch,f4.fp);
}
Fclose(fp);
Fclose(f4.fp);
Remove("abc.txt");
Rename("sy.txt","abc.txt");
}
Myfile myfile::operator+(myfile f2)
{
Myfile f3("abc.txt");
Fp=fopen(fn,"r");
F2.fp=fopen(f2.fn,"w");
Char ch;
While((ch=fgetc(fp))!=EOF)
{
Fputc(ch,f3.fp);
}
Fclose(fp);
While((ch=fgetc(f2.fp))!=EOF)
{
Fputc(ch,f3.fp);
}
Fcloseall();
Return f3
}
Int main()
{
Mylife f1("xyz.txt");
Mylife f2("lmn.txt");
Mylife f3("abc.txt");
Clrscr();
Cout<<"first file\n";
F1.display():
Cout<<"second file\n";
F2.display():
F3=f1+f2;
Cout<<"\n after changes case \n";
!f3;
F3.display();
Getch();
Reurn 0;
};







*****