jeudi 1 mars 2018

Concatenate files using the binary read and write

I wrote a program which takes n number of files and then the user will input their names of the files and then my program will concatenate all those files into 1 file separated by a new-line, here is my program (which works okay):

#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main()
{
    int n;

    cin>>n;

    ifstream read;
    ofstream finalout;
    int i;
    ofstream ("concatn.txt");
    if(!finalout)
    {
        cout << "Oops something went wrong, SORRY DUDE" << endl;
    }
    char a[n][50];
    char help[50];

    for(i=0; i<n; i++)
    {
       scanf("%s", help);
       strcpy(a[i], help);
    }

    string STRING;
    for(i=0; i<n; i++)
    {
        read.open(a[i]);
        if(read == NULL)
        {
            cout << "Could not open the file, SORRY DUDE" << endl;
        }
        while(read.eof() == 0)
        {
            getline(read, STRING);
            finalout << STRING;
        }
        finalout << "\n";
        read.close();
        read.clear();
    }

    finalout.close();

    return 0;
}

Now I have to do the same thing but using the "BINARY READ AND WRITE" but my output file should still be a txt file, how can I do that and can someone explain what does that really mean beacuse I am a bit confused !?

Aucun commentaire:

Enregistrer un commentaire