Copy from one file to another

Write a C++ program to copy all the content from one file to another file. Use get to get the characters from one file and put to copy all the characters to other file.

#include<iostream>
#include<fstream>
using namespace std;
int main(){
	ifstream in;
	ofstream out;
	char ch;
	int count = 0;
	in.open("sample.txt");
	out.open("temp.txt",ios::trunc);
	if(!in.is_open()){
		cout<<"Can't open file";
		return -1;
	}
	cout<<"File Opened"<<endl;
	while(!in.eof()){
		ch = in.get();
		out.put(ch);
		count++;
	}
	cout<<"The total characters are "<<count<<endl;
	in.close();
	out.close();
	cout<<"File Closed"<<endl;
	return 0;
}
output of the above program.
output of the above program.
sample.txt file
sample.txt file
temp.txt file
temp.txt file

You should try to write the program to convert all the lower case characters to uppercase characters and output in a new file.

#include<iostream>
#include<fstream>
using namespace std;
int main(){
	ifstream in;
	ofstream out;
	char ch;
	int count = 0;
	in.open("sample.txt");
	out.open("temp.txt",ios::trunc);
	if(!in.is_open()){
		cout<<"Can't open file";
		return -1;
	}
	cout<<"File Opened"<<endl;
	while(!in.eof()){
		ch = in.get();
		out.put(toupper(ch));
		count++;
	}
	cout<<"The total characters are "<<count<<endl;
	in.close();
	out.close();
	cout<<"File Closed"<<endl;
	return 0;
}

Leave a Reply