• C

Binary File io problem

This should be a really easy question to answer. I'm trying to output binary data to a file using write. Its works ok when I open the file and do the desired output write within one method. However when I try to pass a pointer to the file and use separate methods I get a compile error.

test code is shown below...

#include <iostream>
#include <fstream>
void writeout(ostream *ofile_ptr);

void main()
{
     ofstream outfile("frame.data", ios::out | ios::binary);
     ostream *p = &outfile;
     writeout(&outfile);
}

void writeout(ostream *ofile_ptr)
{
     short int buffer=65535;
     ofile_ptr.write((char *)(&buffer), 2);
}


This is the compile error (gcc used)
In function `void writeout (ostream *)':
btestout.cpp:15: request for member `write' in `ofile_ptr', which is of
non-aggregate type `ostream *'

thanks in advance.
LVL 1
tricanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BlueTrinCommented:
I m not sure about this as this is more about C++ than C.
But shouldn't it be :

ofile_ptr->write((char *)(&buffer), 2);
0
ramyanivCommented:
Since ofile_ptr is a pointer (*ofile_ptr),
It should be ofile_ptr->write and not ofile_ptr.write
0
BlueTrinCommented:
A pointer is the address of where the data is stored (a relative address).

If ofile_ptr was an object from the type: 'ostream'

You could call the method 'write' using ofile_ptr.write ( ... )

But since it is a pointer (on the address of the object of type 'ostream'), you must use:
ofile_pr->write (...)

which is basically the same than:
(*ofile_pr).write (...)

*ofile_pr is the object returned by the address ofile_pr

Got it ?

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tricanAuthor Commented:
got it - thanks a million for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.