I now realize that
OutFile << Myfloat;
just outputs the text equivalent of the value stored in Myfloat.
Currently I'm using a union and writing the 4 bytes as unsigned char's
one at a time, but there has to be a better way.
Thanks
--
Zack Moore-
"Feel free to use it, but don't give me credit -'Anonymous Cynic'
should suffice. I don't want to piss off half the planet - some
of them may be the people who make gasoline, bread, and electricity."
-[Request to quote an Anonymous Cynic]
Zack Moore wrote in message <66femn$q...@mtinsc02.worldnet.att.net>...
>Can anybody tell me if there is a 'nice' and 'C++' way of reading and
>writing a float from a file in binary format?
Ramón Jiménez:
Use functions fopen() and fprintf(), like this:
FILE *fp; /*pointer to file*/
fp=fopen("OutFile", "b"); /*OutFile is a filename, b*/
/* tells C++ file is binary*/
fprintf(fp, "%f", Myfloat'); /* Write MyFloat to bin file OutText
cout.write(&f,sizeof(f));
-----------
When you use << you specify a binary to ASCII conversion. Use
ofstream::write() and ifstream::read() instead.
do this:
float myfloat ;
ofstream out( "file.txt", ios::out ) ;
out.write( (char *)&myfloat, sizeof(float) ) ;
out.close( ) ;
ifstream in( "file.txt", ios::in ) ;
in.read( (char *)&myfloat, sizeof(float) ) ;
in.close( ) :
--
blue skies
michael j. tobler, mailto:mjto...@NOSPAMix.netcom.com
(Remove NOSPAM in return address if replying by email)
Ramón Jiménez wrote:
> Zack Moore wrote in message <66femn$q...@mtinsc02.worldnet.att.net>...
> >Can anybody tell me if there is a 'nice' and 'C++' way of reading and
> >writing a float from a file in binary format?
>
Define an _binary_ i/o streams (derived from "normal" istream and ostream),
and operator << (binary_ostream& osb, float value) etc. ;)
--
Slawomir Marczynski (Mr)
Institute of Physics, Technical University of Szczecin
Al. Piastow 48/49, 70-310 Szczecin, Poland
sla...@arcadia.inter.tuniv.szczecin.pl, tel:+(048-91)-494056
You need to be careful with this. Yes, you can derive a class from
istream and ostream, but this does create one problem. Since the
operator<< and operator>> functions are not declared virtual in
ostream and istream, the following will not quite do what you think:
class MyClass
{
public:
...
friend ostream&
operator<<(ostream& os, const MyClass& myClass)
{
os << intMem;
}
private:
int intMem;
};
...
int
main()
{
binary_ostream bos("somefile.dat");
MyClass anInst;
bos << anInstr; // Unfortunately, this calls ostream::operator<<
// not the binary_ostream<<!!
return(0);
}
What function gets called???
Mike Benzinger
On 10 Dec 97 12:15:08 GMT, sla...@arcadia.tuniv.szczecin.pl (Slawomir
I may have missed something really critical here but, is there a reason that
you cant use the binary flag when you open the file?
file.open("myfile.dat,ios::out|ios::binary);
???
He wanted a "C++" way, i.e. "cout << text" way of doing things. Let's
take a look at the following program:
#include <fstream>
int
main()
{
std::ofstream file("testit.bin", std::ios_base::out |
std::ios_base::binary);
int x = 0x01020304;
file << x;
return( 0 );
}
This small program produces the following results in testit.bin:
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F ASCII
------ -------- -------- -------- -------- -------------
000000 31363930 39303630 16909060
This is not exactly the desired results. As you can see, the values
was still output in ASCII format. The only standard, out-of-the-box,
C++ way to truly write binary data is to use the 'write' function as
follows:
#include <fstream>
int
main()
{
std::ofstream file("testit.bin", std::ios_base::out |
std::ios_base::binary);
int x = 0x01020304;
file.write(reinterpret_cast<char*>(&x), sizeof(x));
return( 0 );
}
This produces the correct results:
Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F ASCII
------ -------- -------- -------- -------- -------------
000000 04030201 ····
NB: The PC uses little-endian byte order, hence the order of the bytes
is backwards.
Mike Benzinger
#include <fstream.h>
main()
{
float num;
fstream f1("mydata.bin",ios::in|ios::binary);
if( !f1 )
{
cerr << "NO Way Jose" << endl;
return 1;
}
f1.read( &num, sizeof( float ) );
}
Michael J. Tobler <mto...@ibm.net> wrote in article
<348EA12C...@ibm.net>...
> This is WAY overboard ... the way to
> do is with existing classes and their
> respective member functions. Please see
> my other post within this thread for the answer.
> --
> blue skies
> michael j. tobler, mailto:mto...@no-spam-ibm.net
> (Remove the "no-spam-" in return address)
>
> Slawomir Marczynski wrote:
>
> > : Zack Moore wrote:
> > : >
> > : > Can anybody tell me if there is a 'nice' and 'C++' way of reading
and
> > : > writing a float from a file in binary format?
> >
> > Define an _binary_ i/o streams (derived from "normal" istream and
ostream),
> > and operator << (binary_ostream& osb, float value) etc. ;)
> >