Refer to the little rookie Colonel's blog
1 //file Operat.cpp: Defines the entry point of the console application. 2 /*The main function of the above example is to copy the contents of one file into another file,3 This function is mainly implemented by a function copy. It contains two arguments of type string, S and D, which means that the contents of the file s are copied to file D. 4 first, two file streams are declared, Ifstream infile and Ofstream outfile, and then the open method of the call stream opens the file,5 and check to see if there is a problem in the open process. If there is a problem then error and return, otherwise, proceed to start copying. 6 as you can see, each time we take the contents of the source file out of a row into a temporary string variable temp, and then write the contents of temp to the destination file. 7 The function getline is a top-level function that reads a line from the input stream and puts it into a string variable. */8 9 //Ten One#include"stdafx.h" A#include <fstream> -#include <iostream> -#include <string> the - using namespacestd; - - + voidCopystringSstringD//A parameter of type String that represents the copy of the contents of S to D - { + //declaring two file input and output streams A ifstream infile; at ofstream outfile; - -Infile.open (S.c_str ());//Open File - if(!infile) - { -cout <<"File:"<<"Not find!"<< Endl;//error if the original file does not exist in return; - } to Outfile.open (D.c_str ()); + if(!outfile) - { thecout <<"File:"<< D <<"Not find!"<<Endl; * return; $ }Panax Notoginseng - stringtemp =""; the while(Getline (infile, temp))//Copy the contents of the infile to temp + { Aoutfile << Temp <<"\ n";//display the copied temp content in outfile the } + - //Close File $ infile.close (); $ outfile.close (); -cout <<"One file copy finished!"<<Endl; - } the - Wuyi voidMain () the { - stringSource ="C:\\users\\administrator\\desktop\\source.txt"; Wu stringDestination ="C:\\users\\administrator\\desktop\\destination.txt"; - copy (source,destination); About $}
Note that the path to the file is written: vs is a double slash \ \, not a single slash \ ...
Pro-Test can be run .....
Basic knowledge of C + + (1)----file operations