C++にて、ファイルコピーを行なう関数を作ろうと思ったのですが
いくつかの方法がありどの方法でやるかを迷っています
皆さんならどの方法で実装するかを教えてください
それぞれの長所などがいまいち分からず選びきれていない状況です
よろしくお願いします
サイトに載っていたコードを参考にしています
C++
1//system関数を利用した方法 2system( "cp test.txt /tmp" ); 3
C++
1void CopyFile( const char *from_file_name, const char *to_file_name ) 2{ 3 //streambuf_iteratorとcopy()を利用した方法 4 5 ifstream is( from_file_name, ios::in | ios::binary ); 6 ofstream os( to_file_name, ios::out | ios::binary ); 7 8 // ファイルコピー 9 istreambuf_iterator<char> iit(is); 10 istreambuf_iterator<char> end; 11 ostreambuf_iterator<char> oit(os); 12 copy( iit, end, oit ); 13}
c++
1void CopyFile( const char *from_file_name, const char *to_file_name ) 2{ 3 //fstream::rdbuf()と<<を使った方法 4 ifstream is( from_file_name, ios::in | ios::binary ); 5 ofstream os( to_file_name, ios::out | ios::binary ); 6 7 // ファイルコピー 8 os << is.rdbuf(); 9}
c++
1void my_copy(char *from[], char *to[]) { 2 //C言語標準ライブラリを利用した方法 3 FILE *f1, *f2; 4 int c; 5 6 f1 = fopen(from, "r"); 7 if (!f1) err(from); 8 9 file_exist(to); 10 f2 = fopen(to, "w"); 11 if (!f2) err(to); 12 13 while ((c = fgetc(f1)) != EOF) { 14 if (fputc(c, f2) == EOF) err(to); 15 } 16 17 fclose(f2); 18 fclose(f1); 19}
こんな方法があるよ
こっちの方が安全だよ、という情報がありましたら
よろしくお願いします
---追記
C++17は私の環境では使用できないのでstd::filesystem::copyの方法は無しでお願いします

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/02 00:20