File Handling

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<fstream.h>
#include<stdlib.h>
void main()
 {
clrscr();
ofstream out;
ifstream in;
char ch,fname1[10],fname2[10];
cout<<"Enter the file name to be copied:";
cin>>fname1;
cout<<"Enter the new file name:";
cin>>fname2;
out.open(fname1);
out<<"This is a test program.";
out.close();
in.open(fname1);
if(in.fail())
       {
cerr<<"No such a file exists.";
getch();
exit(1);
       }
out.open(fname2);
if(out.fail())
       {
cerr<<"Unable to create the file.";
exit(1);
getch();
       }
while(!in.eof())
       {
ch=(char)in.get();
out.put(ch);
       }
in.close();
out.close();
cout<<"The file is copied.\n";
in.open(fname2);
cout<<"The copied contents are:";
while(!in.eof())
 {
ch=(char)in.get();
cout<<ch;
 }
in.close();
getch();
 }

No comments:

Related Posts with Thumbnails