通过终端接收 3 个文件,并使用 fstream 将 file2 和 file3 的内容连接到文件 1

Recieve 3 files by terminal and concatenate the content of file2 and file3 to file1 using fstream

本文关键字:文件 file3 file2 连接 fstream 端接 终端      更新时间:2023-10-16

我在C++中乞求,所以可能是我的代码的一部分没有意义,对不起。

我要做的就是(C++,Linux,通过fstream(:

·通过以下方式接收终端传递的 3 个或更多文件: ./可执行文件 1.txt 文件 2.txt 文件3.txt

· 编程一个函数,用于读取文件 File2.txt 和 File3.txt并将其复制到 File1.txt(连接,不要覆盖(

我不知道该怎么做,我对fstream一无所知,我现在只是自己学习,所以我真的需要帮助。也许SO中解决了类似的问题,但我不知道如何通过它们解决我的问题。

附上我拥有的代码。我不知道如何编写函数,所以它是空的。

非常感谢。

我尝试做:

void concat(char *argv[], int numberoffilesreceived){
    char c;
    towritethefiles.open(argv[0], ios::app);
    for(int i=1; i<numberoffilesreceived; i++){
        toreadthefiles.open(argv[i], ios::in);
        while(!toreadthefiles.eof()){
            toreadthefiles >> c;
            towritethefiles<< c;
        }
    }
}

它可以编译但不工作,当您运行它时程序会冻结。

我也尝试使用 std::copy by 我不明白它是如何工作的。

ifstream toreadthefiles;
ofstream towritethefiles;
void concat(char *argv[], int numberoffilesreceived);
int main(int argc, char *argv[]){
    /* 1/2 The code from below to 2/2 it's only to prevent path errors when receiving the files (it works fine) */
    const char directory[SIZE]="./";
    int count_files=0;
    char files[SIZE][SIZE];
    for(int i=1; i<argc; i++){
        strcpy(files[i], directory);
        strcat(files[i], argv[i]);
        count_files++;
    }
    /*2/2 to add ./ to the name files when passed by terminal: ./executable ./file1.txt ./file2.txt ./file3.txt */
    /*check if received almost 3 files like required */
    if(argc<3){
        cout<< "Error, to few files entered" << endl;
        getchar();
        exit(1);
    }
    /*pass the files to the concat function*/
    for(int i=1; i<argc; i++){  
        concat(&argv[i], count_files);      
    }

    toreadthefiles.close();
    towritethefiles.close();
    return 0;
}
void concat(char *argv[], int count_files){
}

我想我发现你的concat()函数有问题。您正在为传入的每个文件调用concat()。然后在函数中,您将使用 count_files 再次运行该循环以获取传入的文件数。

我会考虑重写concat()函数,使其看起来像这样:

void concat(std::ofstream& outputStream, char* fileToAppend)
{
    std::ifstream in(fileToAppend);
    if (!in) {
        cout << "Error, cannot open file: " << fileToAppend;
        return;
    }
    // Use std::getline to read each line in the input stream,
    // then write it to the output stream!
    string line;
    while (std::getline(in, line)) {
        outputStream << line;
    }
}

好处是您可以重用将单个输入文件附加到现有输出流的函数,并完成检查以确保文件存在(您可能需要更复杂的内容,例如在要追加的文件上返回 true/false,或抛出错误等(。

main() 中,您将在检查至少三个文件后将代码替换为以下内容:

    // Create an output stream with the first file
    // ios::out- output flag
    // ios::app- append flag
    std::ofstream out(argv[1], ios::out | ios::app);
    // Make sure the file exists!
    if (!out) {
        cout << "Error, cannot open file: " << argv[1];
        exit(1);
    }
    // For all other arguments, concat with the first.
    for (int i = 2; i < argc; i++) {
        concat(out, argv[i]);
    }
您可以将

std::copy与流迭代器一起使用,并且我已经修改了我之前对它会很慢的怀疑,所以这里有一种方法是在代码中对注释执行此操作。

#include <iostream>
#include <fstream>
#include <vector>
#include <ios>
#include <stdexcept>
void concat(const std::string& destination, const std::vector<std::string>& sources) {
    // open the destination file and keep it open until all is done
    std::ofstream dest_fs(destination, std::ios_base::binary);
    if(!dest_fs) 
        throw std::runtime_error("Could not write to "" + destination + "".");
    // loop over the source files
    for(const auto& source_file : sources) {
        // open the current source file
        std::ifstream source_fs(source_file, std::ios_base::binary);
        if(!source_fs)
            throw std::runtime_error("Could not read from "" + source_file + "".");
        // copy from source to destination
        std::copy(std::istreambuf_iterator<char>(source_fs),
                  std::istreambuf_iterator<char>(),
                  std::ostreambuf_iterator<char>(dest_fs));
    }
}
int cppmain(std::string program, std::vector<std::string> args) {
    if(args.size() < 2) {
        std::cout << "USAGE: " << program << " destination_file input_file(s)n";
        return 1;
    }
    // extract the first argument which is the destination file
    std::string destination_file = std::move(args.front());
    args.erase(args.begin()); // erase first argument from the vector
    try {
        // do the concatenation
        concat(destination_file, args);
        return 0;
    } catch(const std::exception& ex) {
        std::cerr << program << ": ERROR: " << ex.what() << "n";
        return 1;
    }
}
int main(int argc, char* argv[]) {
    return cppmain(argv[0], {argv + 1, argv + argc});
}