简体   繁体   English

C++ 复制另一个文件

[英]C++ copying another file

i was wondering how to use c++ ifstream/ofstream to copy a file and save it as another name.我想知道如何使用 c++ ifstream/ofstream 复制文件并将其另存为另一个名称。

this is as far as i got.这是我得到的。 I know how to get the file, its just that i don't know how to copy that file and save it as a different name.我知道如何获取文件,只是我不知道如何复制该文件并将其另存为不同的名称。

#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 with overloaded << is standard way to go.重载<< 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();

Basically you want to read a character at a time and write said character to the output stream.基本上,您想一次读取一个字符并将所述字符写入输出流。 There's a get() overload which accepts a streambuf output variable that would work.有一个 get() 重载,它接受一个可以工作的 streambuf 输出变量。 You could also use the example on cplusplus.com rdbuf documentation.您还可以使用 cplusplus.com rdbuf 文档中的示例。

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

This code below should give you a sense of what you want to do.下面的代码应该让您了解您想要做什么。

There are few things you should keep in mind, for example:您应该记住几件事,例如:

  • is the path of the file giving to read is valid?提供给读取的文件的路径是否有效?
  • or do you want to save the data from an output file if that file exists, before pushing new data?.或者你想在推送新数据之前保存输出文件中的数据(如果该文件存在)?

You could test this code by just creating a file into your desktop or any location, just change the filePath and destinationPath variables then run the code.您可以通过在桌面或任何位置创建一个文件来测试此代码,只需更改filePathdestinationPath变量,然后运行代码。 (c++ 11) (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;
}

This is not the only way to do this, but this code should put you on a direction这不是执行此操作的唯一方法,但是此代码应该为您指明方向

Copy a file and save it on another file:复制一个文件并将其保存在另一个文件中:

#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