Re: Parse binary stream to long ints
On Feb 28, 1:51 am, "John Brawley" <jgbraw...@charter.net> wrote:
"John Brawley" <jgbraw...@charter.net> wrote in message
[...]
...And here's my new problem.
(I hate pointers; I barely understand them)
Then you probably shouldn't be using them. Using anything you
don't understand can be dangerous, but pointers are particularly
so.
, but I was able to implement
your (James') suggestion and reinterpret_cast the infile.read() function s=
o
I could use read() with long ints. Now I have this weird result:
#include <fstream>
#include <cstdlib>
using namespace std;
ifstream inrn;
ofstream ourn;
long int * stuff[1];
Note that this is an array of one pointer. I'm not sure that
that's what you want.
int f;
int main() {
//next line works on *any* windows file....
//(test by sticking any filename in there)
inrn.open("random.dat", ios::binary);
ourn.open("outrn.txt");
inrn.read(reinterpret_cast<char*>(stuff),4);
And now you're likely to be running into deep trouble. C++
handles arrays a bit funny. (It's better to avoid them
entirely, and just use std::vector, but sometimes, we don't have
the choice.) In particular, it converts the array to a pointer
to the first element in a lot of cases. Including this one. So
what you're doing here is reading external bytes into a pointer.
Which is definitely not recommended.
Possibly, you didn't want the pointer to begin with:
long int stuff[ 1 ] ;
inrn.read( reinterpret_cast< char* >( stuff ), sizeof stuff ) ;
will do the trick: the implicite conversion of the array into a
pointer (a long int*) gives you something on which the
reinterpret_cast is legal, and will do what is wanted.
cout<<stuff[0]; //...HEX!!
As you've written the code, you're asking for a pointer to be
output: stuff is an array of pointers, and stuff[0] is the first
pointer. The format cout uses to write pointers is very system
dependent, but hex is a popular choice. (One system I once
worked on would print them as two hex values, separated by a
colon.)
ourn<<*stuff; //writes same hex, to file
Well, *stuff and stuff[0] are perfectly equivalent in C++.
Again, this is an oddity of C++ (inherited from C): the indexing
operator is defined in terms of pointer arithmetic, i.e. a[b] is
defined as being the same a *(a+b). Indexing only works on
pointers, and in the case of something declared as an array, it
works because an array converts implicitly to a pointer.
Perhaps what you want is more like:
std::vector< long int > v( someSize ) ;
inrn.read( reinterpret_cast< char* >( &v[ 0 ] ),
sizeof( long int ) * v.size() ) ;
If you use std::vector, it will actually act like an array, and
not like a pointer.
inrn.close();
ourn.close();
return 0;
}
It's all in hexadecimal.
Do I actually have to write a converter to get the long int I
want out of the hexadecimal?
No. You have to read and write long int's, and not pointers to
long ints.
I've redone many (*many*) things in this, and I believe I'm
sure I'm not getting an address, but stuff[] is actually
getting a hexadecimal representaiton of the long int I'm
trying to achieve....
You're getting the contents of the pointer. You initialize the
pointer by reading bytes into it from a file, and I certainly
wouldn't recommend dereferencing it:-). But as far as C++ is
concerned, it's a pointer, and << will treat it like a pointer.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34