Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
2.9k views
in Computer by (69.4k points)

Write a program that copies one file to another. Has the program to take the file names from the users? Has the program to refuse copy if there already is a file having the target name?

1 Answer

0 votes
by (61.3k points)
selected by
 
Best answer

#include<iostream.h>

#include<conio.h>

#include<fstream>

#include<stdlib.h>

void main()  { 

 ofstream outfile;

ifstream infile;

char fname1[10],

fname2[20]; 

char ch,uch; 

 clrscr( );

cout<<"Enter a file name to be copied ";  

cin>> fname1; 

 cout<<"Enter new file name";  

cin>>fname2; 

 infile.open(fname1); 

if( infile.fail( ) )

{

cout<< " No such a file Exit";

getch();

exit(1);

}

outfile.open(fname2,ios::noreplace);

if(outfile.fail())

{

cout<<"File Already Exist"; 

getch(); exit(1);

}

else

{

while(!infile.eof( ))

{

ch = (char)infile.get( );

outfile.put(ch);

}

 infile.close( ); 

 outfile.close( ); 

 getch( );

}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...