Cから見たC++の(八)ファイルフロー操作

1400 ワード

Linuxプラットフォームの下で標準的なCライブラリAPIを提供してファイルに対する読み書き操作を実現し、同様にC++も自身のファイルストリームに対する操作のいくつかの手段を提供した.後で使うかどうか分かりませんが、理解する必要があります.
ソース:
#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;
}
C++でファイルストリームに対する操作を実現するには、ヘッダファイル「includeHello,SE7EN: Remember,hope is a good thing! コンパイル実行:
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#