Использование функций Read () и Write () для объединения двух файлов - PullRequest
0 голосов
/ 01 декабря 2019

В настоящее время я изучаю, как использовать функции read () и write () для реализации программы, которая объединяет файлы.

Я объединяю текстовый файл с именем «NotesBetweenTwoSisters2.txt» в конец другого текстового файла с именем «NotesBetweenTwoSisters1.txt». (Так что теперь NotesBetweenTwoSisters1.txt имеет больший размер файла.)

"NotesBetweenTwoSisters1.txt"

Madam, 

Keep an eye on the radar. If it doesn't look that good for kayak paddling tonight, then the next time which we can rent the kayak is tomorrow noon. 
But at that time you will have to take care of the pup, and only your darling and I can enjoy the trip. 

Sis

"NotesBetweenTwoSisters2.txt"


Hi sis,

I only deal with loyal ones. Obviously the pup is much more loyal. You and I really see eye to eye on this issue. Enjoy your kayak paddling.

"Your Madam"

Я уже написал код, который выводит оба содержимого файла на терминал, как и ожидалось, но я не уверен, правильно ли я делаю конкатенацию. Если кто-нибудь может помочь мне в том, как правильно использовать функции read () и write () для объединения файлов, я был бы очень признателен!

Текущий программный код

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    fstream inFile1;
    fstream inFile2;
    fstream outFile;

    inFile1.open("NotesBetweenTwoSisters1.txt", ios::in|ios::binary);
    inFile2.open("NotesBetweenTwoSisters2.txt", ios::in|ios::binary);

    inFile1.seekg(0, inFile1.end);
    int size1 = inFile1.tellg();
    inFile1.seekg(0, inFile1.beg);

    // cout << "File #1 Size: " << size1 << endl;
    char arr1[size1];

    inFile1.read(arr1, sizeof(arr1));
    inFile1.close();

    inFile2.seekg(0, inFile2.end);
    int size2 = inFile2.tellg();
    inFile2.seekg(0, inFile2.beg);

    // cout << "File #2 Size: " << size2 << endl;
    char arr2[size2];

    inFile2.read(arr2, sizeof(arr2));
    inFile2.close();

    outFile.open("outputFile.dat", ios::out|ios::app|ios::binary);
    outFile.write(arr1, sizeof(arr1));
    outFile.write(arr2, sizeof(arr2));

    for(int count = 0; count < size1; count++)
    {
        cout << arr1[count] << "";
    }

    for(int count = 0; count < size2; count++)
    {
        cout << arr2[count] << "";
    }

    cout << endl;
    outFile.close();

    return 0;
}

Токовый выход

Madam,

Keep an eye on the radar. If it doesn't look that good for kayak paddling tonight, then the next time which we can rent the kayak is tomorrow noon.
But at that time you will have to take care of the pup, and only your darling and I can enjoy the trip.

Sis
Hi sis,

I only deal with loyal ones. Obviously the pup is much more loyal. You and I really see eye to eye on this issue. Enjoy your kayak paddling.

"Your Madam"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...