raptr_dflo 48 Posting Pro

Did you scroll down far enough to find the link to this? Somewhere in there is probably some help as far as how to use the library....

raptr_dflo 48 Posting Pro

I can't tell you why fil1.write() and fil1.read() aren't working, without more information. What values are you entering for e1? What file gets generated when you write() it? What is in e1 after you read() it back?

I don't know what your professor meant by "write it via object." To me that means something more like:

class emp
{
...
    void write(ofstream & fil) const {
        fil << name << " " << num << " " << dep << " " age << endl;
    }
    void read(ifstream & fil) {
        fil >> name >> num >> dep >> age;
    }
...
};

so that later you can do:

e1.write(fil1);
    e2.read(fil2);

There's nothing wrong with your logic, if it addresses your assignment. However, what you specified is

question:-
Write a C++ program to merge the content of two files namely “Employee.txt”
and “Salary.txt” into “EmployeeDetails.txt”.

and what you've programmed does not do that, it generates all three files at the same time. I'm just trying to help.

As far as writing out binary files (using read() and write() the way you are so far), I think you need to open the files in "binary" mode. Include flag ios::binary when you open the file. And if the only operation you're performing on a file is writing (or reading), then don't specify the other flag, just ios::out (or ios::in), not both.

raptr_dflo 48 Posting Pro

You need to read the file in "binary mode". While you can probably do the same thing with fstream, I honestly have never bothered learning how. The C way to do it uses fread() on a FILE* object:

#include <stdio.h>
#include <iostream>
using namespace std;

...

FILE *fp = fopen("C:/Users/DFlo/Pictures/13.jpg", "rb");
unsigned char magic[4];
fread((void *)magic, 1, 4, fp);
cout << hex << "magic:";
for (int i = 0;  i < 4;  i++)
    cout << " 0x" << int(magic[i]);
cout << dec << endl;

Note that fopen() return NULL on failure, and fread() returns the number of items read (should equal 4 in this case).

raptr_dflo 48 Posting Pro

Cool! If/when you're all set, pease mark this thread as "Solved". :)