C C + + (eight) file stream operation

Source: Internet
Author: User

The standard C library API is provided under the Linux platform to read and write files, and C + + also provides its own means for file stream operation. Although I do not know whether it will be used later, but it is necessary to understand.

Source:

#include <iostream>
#include <fstream>

using namespace std;

int main (void)
{       char filename1[256],filename2[256];
        cout<< "Input source file name:  ";
        cin>>filename1;
        cout<< "Input destination file name:  ";
        cin>>filename2;
        Ifstream infile (filename1);
        Ofstream outfile (filename2);
        char ch;
        while (Infile.get (CH))
                outfile.put (CH);
        Infile.close ();
        Outfile.close ();

        return 0;
}
In C + + to implement the operation of the file stream, you need to include the header file "include <fstream>". This program needs to copy the contents of a file into another file, for example, we have an existing file called Source.txt, which has the following content:

Hello,se7en:
	Remember,hope is a good thing!
Compile run:

root@se7en-lifebook-lh531:~/learn/cpp_program# g++ file.cpp-o file
root@se7en-lifebook-lh531:~/learn/cpp_ program#./file 
Input source file name:  source.txt
Input destination file name:  dest.txt
root@se7en-lifebook-lh531:~/learn/cpp_program# Cat Dest.txt 
hello,se7en:
	remember,hope is a good thing!
root@se7en-lifebook-lh531:~/learn/cpp_program# 


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.