C++ program to copy one file to anothere file

Saturday, March 13th, 2010

AIM
A program to copy one file to another file and convert the lower case characters to upper case characters.

ALGORITHM

1) Start the process
2) Create the input file and out put file.
3) Get the input file name to fname1.
4) Get the output file name to fname2.
5) Open the infile(fanme1)
6) Check if infile.fail()
a) True:
i) Execute error conversion
ii) exit
7) open the outfile(fanme2)
8) check if outfile.fail()
a) repeat step(6:a)
9) check while infile.eof()
a) True:
i) Ch?(char)infile.get()
10) Close both files
11) Stop the process.

PROGRAM :

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
#include<ctype.h>
#include<fstream.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( ) )
	{
		cerr<< " No such a file Exit";
		getch();
		exit(1);
	}
	outfile.open( fname2);
		if(outfile.fail( ))
		{
			cerr<<"Unable to create a file";
			getch();
			exit(1);
		}
	while( !infile.eof( ) )
	{
		ch = (char) infile.get( );
		uch = toupper(ch);
		outfile.put(uch);
	}
	infile.close( );
	outfile.close( );
	getch( );
}

OUTPUT:

Enter a file name to be copied.
C:\text1.txt
Enter new file name
D:\new.txt

Input file
Asbcdefghijklmnopqrstuvwxyz
Output file
ASBCDEFGHIJKLMNOPQRSTUVWXYZ

Author Name :
Ranjith

Total : 2 Comments


2 Responses to “C++ program to copy one file to anothere file”

  1. Bharath says:

    Hi all, how to call a program from another program and how to execute
    more than one function at a time.
    I am new to c++ concepts, so please any one help me by providing
    code….

  2. eusto says:

    What happens if i pass the same name to source and destination? What happens if source is a shortcut(link) to destination. Your program does not handle this situation correctly.

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: