?
Solved

Binary File io problem

Posted on 2003-03-02
4
Medium Priority
?
234 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:trican
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:BlueTrin
ID: 8053372
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
 

Expert Comment

by:ramyaniv
ID: 8053485
Since ofile_ptr is a pointer (*ofile_ptr),
It should be ofile_ptr->write and not ofile_ptr.write
0
 

Accepted Solution

by:
BlueTrin earned 200 total points
ID: 8053993
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
 
LVL 1

Author Comment

by:trican
ID: 8054130
got it - thanks a million for your help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question