0

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;
    finalout.open("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 !?

Dan
  • 139
  • 1
  • 8
  • A) `char a[n][50];` this is a non-portable extension of your compiler, VLAs are not standard B) If you have something that works why do you "need to do the same thing using BINARY READ AND WRITE" ? C) [Difference between files writen in binary and text mode](https://stackoverflow.com/q/229924/583833) – Borgleader Mar 01 '18 at 19:16
  • Its part of my assigment for homework that's why :), and the link you suggested is for C, I need a CPP implementation I guess – Dan Mar 01 '18 at 19:18
  • `finalout << read.rdbuf();` will write all the contents of `read` into `finalout` in one call. No need for any loop. – Igor Tandetnik Mar 01 '18 at 19:20
  • After `cout << "Oops something went wrong, SORRY DUDE" << endl;` should you do a return/exit? i.e. no point carrying on – Ed Heal Mar 01 '18 at 19:27
  • `while(read.eof() == 0)` is bound to be wrong - see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Ed Heal Mar 01 '18 at 19:28
  • @EdHeal Yes of course, I mean I know that but I am focused on the binary thing first and then I ll add to everything the exit codes etc, but it seems like everybody is giving me feedback on this code instead of telling me something for my actual question :/ – Dan Mar 01 '18 at 19:29
  • Read up http://www.cplusplus.com/reference/istream/istream/read/ and the write version – Ed Heal Mar 01 '18 at 19:31
  • Why are you using `scanf` in C++ code? `cin` exists – Ed Heal Mar 01 '18 at 19:32

1 Answers1

2

Binary read-write is just to support plateform-independent read-write support. For example, on Windows platform New Line is \r\n but for UNIX based platform it is just \n. So, When we are reading in binary mode the characters will be read as it is. But if you are reading it in non binary mode then characters will be modified if needed.

To open a file in binary mode use ios::binary flag.

ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);

See here for more info

cse
  • 4,066
  • 2
  • 20
  • 37