Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Binary File io problem

Posted on 2003-03-02
Medium Priority
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;

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.
Question by:trican
  • 2

Expert Comment

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);

Expert Comment

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

Accepted Solution

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 ?


Author Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

577 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