C 复制另一个文件

C++ copying another file

本文关键字:文件 另一个 复制      更新时间:2023-10-16

我想知道如何使用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();

复制文件并将其保存在另一个文件上:

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

基本上您想一次读取一个字符,然后将上述字符写入输出流。有一个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;
}

这不是这样做的唯一方法,但是此代码应该将您置于方向

相关文章: