簡體   English   中英

C++ 復制另一個文件

[英]C++ copying another file

我想知道如何使用 c++ ifstream/ofstream 復制文件並將其另存為另一個名稱。

這是我得到的。 我知道如何獲取文件,只是我不知道如何復制該文件並將其另存為不同的名稱。

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

重載<< rdbuf是標准方法。

  ifstream src;
  ofstream dst;

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

  src.close();
  dst.close();

基本上,您想一次讀取一個字符並將所述字符寫入輸出流。 有一個 get() 重載,它接受一個可以工作的 streambuf 輸出變量。 您還可以使用 cplusplus.com rdbuf 文檔中的示例。

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

下面的代碼應該讓您了解您想要做什么。

您應該記住幾件事,例如:

  • 提供給讀取的文件的路徑是否有效?
  • 或者你想在推送新數據之前保存輸出文件中的數據(如果該文件存在)?

您可以通過在桌面或任何位置創建一個文件來測試此代碼,只需更改filePathdestinationPath變量,然后運行代碼。 (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;
}

這不是執行此操作的唯一方法,但是此代碼應該為您指明方向

復制一個文件並將其保存在另一個文件中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM