Basic knowledge of C + + (1)----file operations

Source: Internet
Author: User
Tags file copy

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.