2017-10-03 1 views
1

Ich frage mich, wie ich C++ ifstream/ofstream verwenden, um eine Datei zu kopieren und als einen anderen Namen zu speichern.C++ Kopieren einer anderen Datei

das ist so weit wie ich habe. Ich weiß, wie man die Datei bekommt, nur dass ich nicht weiß, wie ich diese Datei kopieren und als einen anderen Namen speichern soll.

#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; 
} 

Antwort

0

Grundsätzlich möchten Sie ein Zeichen nach dem anderen lesen und dieses Zeichen in den Ausgabestrom schreiben. Es gibt eine get() - Überladung, die eine Streambuf-Ausgabevariable akzeptiert, die funktionieren würde. Sie können das Beispiel auch auf der Dokumentation zu cplusplus.com rdbuf verwenden.

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

+0

http://coliru.stacked-crooked.com/a/be544185115d1b42 – sehe

0

Dieser Code unten sollte Ihnen ein Gefühl von dem, was Sie tun möchten.

Es gibt nur wenige Dinge, die Sie im Auge, zum Beispiel halten sollen:

  • der Pfad der Datei gibt gültig zu lesen ist?
  • oder möchten Sie die Daten aus einer Ausgabedatei speichern, wenn diese Datei existiert, bevor neue Daten übertragen werden ?.

Sie können diesen Code testen, indem Sie nur eine Datei in Ihrem Desktop oder einem beliebigen Ort erstellen, ändern Sie einfach die filePath und destinationPath Variablen dann den Code ausführen. (C++ 11)

#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; 
} 

Dies ist nicht der einzige Weg, dies zu tun, aber dieser Code sollten Sie sich auf eine Richtung gesetzt

0

Kopieren einer Datei und speichern Sie sie auf eine andere Datei:

#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

neugierig, was Ihr Code mit binären tun würde (sagen wir, komprimiert) Datei (en) –

1

rdbuf mit überladen << ist Standard Weg zu gehen.

ifstream src; 
    ofstream dst; 

    src.open("from", ios::in | ios::binary); 
    dst.open("toto", ios::out | ios::binary); 
    dst << src.rdbuf(); 
Verwandte Themen