Copy File

C++ Programming

C++ Programming Examples

#include<iostream>
#include<fstream>
using namespace std;

int main() {
	int size = 20;
	char line[size];

	/* Connect 'data.txt' to 'wfile' */
	ifstream rfile("data.txt");
	ofstream wfile("new_data.txt");
	cout << "Copping data from one file to another..." << endl;

	/* Check end of file */
	while(rfile) {
		/* Read a line */
		rfile.getline(line,size);

		/* Write the line */
		wfile << line << endl;
	}

	cout << "File Copied!" << endl;

	/* Disconnect files from 'wfile' and 'rfile' */
	rfile.close();
	wfile.close();
	return 0;
}


          /****** Output ******/
          

Copping data from one file to another...


File Copied!

Learn more about Input/Output in C++