2017-10-03 6 views
1

私は、ファイルをコピーして別の名前として保存するために、ifstream/ofstreamのC++を使用する方法を知りました。別のファイルをコピーするC++

これは私が持っている限りです。私はファイルを取得する方法を知っています。そのファイルをコピーして別の名前で保存する方法はわかりません。

#include<iostream> 
#include<fstream> 
#include<string> 
    namespace std; 
int main() 
    { 
    ofstream 
    ifstream 
    cout << "enter your file you want to copy"<< endl; 
    cin >> input_file_name; 
    in_file.open(input_file_name); 
    if (!in_file) 
    { 
    cout <<" there is no such file"<<endl; 
    return 0; 
    } 
    cout <<" enter the name you want to save this copy file"<<endl; 
    cin >> output_file_name; 
    out_file.open(output_file_name); 
    if (!out.file) 
    { 
    cout<<"file is not available"<<endl; 
    return 0; 
    } 
    in_file.close(); 
    out_file.close(); 
    return 0; 
} 

答えて

0

基本的には、一度に1文字を読み込み、その文字を出力ストリームに書きたいとします。動作するstreambuf出力変数を受け入れるget()オーバーロードがあります。この例は、cplusplus.com rdbufのマニュアルでも使用できます。

http://www.cplusplus.com/reference/fstream/ofstream/rdbuf/

+0

あなたのコードは、バイナリ(たとえば、圧縮された)ファイル(複数可)とどうなるのかhttp://coliru.stacked-crooked.com/a/be544185115d1b42 – sehe

0

以下このコードは、あなたが何をしたいのかの感覚を与える必要があります。

あなたはたとえば、心に留めておくべきいくつかの事柄があります:

  • は読み取りに与えたファイルのパスが有効ですか?
  • または、新しいデータをプッシュする前に、そのファイルが存在する場合は出力ファイルからデータを保存しますか?

あなただけのコードを実行し、その後filePathdestinationPathの変数を変更、ちょうどあなたのデスクトップまたは任意の場所にファイルを作成することにより、このコードをテストすることができます。 (++ 11 C)

#include <iostream> 
#include <fstream> 
#include <vector> 

using namespace std; 

vector<string> readFromFile(const char *filePath) { 
    vector<string> container; 
    ifstream obj(filePath); // automatically our file would be open 
    if (obj.is_open()) { // we check anyways 
     string line = ""; 
     while(getline(obj, line)) { 
      if (!line.empty()) // prevent us to insert empty line into our vector 
      container.push_back(line); 
     } 
     obj.close(); // close after we finish reading to avoid corruption 
    } 
    return container; 
} 

bool pipingToDestination(vector<string>data, const char *filePath) { 
    std::filebuf fb; fb.open(filePath,std::ios::out); // open the file 
    ostream obj(&fb); 
    if (!data.empty() && fb.is_open()) { // make sure we have some data && the file file is open to write 
     for (string x: data) { // c++11 
      obj << x << endl; 
     } 
     fb.close(); 
     return true; 
    } 
    return false; 
} 

int main() { 
    string filePath = "/Users/lamar/Desktop/testFile.txt"; 
    vector<string> data = readFromFile(filePath.c_str()); 

    cout << "File has passed data into container ... \n"; 

    for(string x: data) { 
     cout << x << endl; 
    } 

    cout << "Creating destination file \n"; 
    string destinationPath = "/Users/lamar/Desktop/destFile.txt"; 
    cout << "has piped data into file " << boolalpha << pipingToDestination(data, destinationPath.c_str()); 

    return 0; 
} 

これは、これを行うための唯一の方法はありませんが、このコードは方向

0

にあなたを置くべきファイルをコピーして別のファイルに保存:

#include <fstream> 
#include <iostream> 
#include <string> 

int main(int arc, char* argv[]) { 
    std::ifstream file1(argv[1]); 
    std::ofstream file2(argv[2]); 
    std::string line; 
    if (file1.good() && file2.good()) { 
     while (getline(file1, line)) { 
      file2 << line; 
      file2 << '\n'; 
     } 
    } 
    file1.close(); 
    file2.close(); 
} 
+0

は好奇心 –

1

rdbufオーバーロードされた<<は標準的な方法です。

ifstream src; 
    ofstream dst; 

    src.open("from", ios::in | ios::binary); 
    dst.open("toto", ios::out | ios::binary); 
    dst << src.rdbuf(); 
関連する問題