C++ program to copy one file to anothere file

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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

  1. 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….

    Reply
  2. 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.

    Reply

Leave a Reply