Hopefully simple question, input streams

This is a discussion on Hopefully simple question, input streams within the C++ Programming forums, part of the General Programming Boards category; Hopefully this is a simple question. I searched out for it, but found nothing. Basically I just need to know ...

then it hits a line with a space, ( I can see the raw file as well, and in a hex editor it ignores these, so I can assume there is a way to decide what is a is not a space / \n), and then gets a little funky...

This should come out as normal, but when I hit the spaces it comes out more like this...

0000 0106 0DFFFFEEFFFF05AB

which to me seems impossible, but hey, I am no expert either .

Any simple mistakes anyone sees with that? I am hesitant to eradicate all FF's ( space in ascii ) because there are of course numbers which have FF in them. Any ideas on this? I am kinda not able to spot it, but perhaps someone has a good way to id them. Thanks!

Last edited by dpro; 03-08-2006 at 09:41 PM.
Reason: Well it would help to get the code correct it would seem!

No I didn't write it myself. I can hex edit it, its generated and readable in a separate program, so I know this one is "correct". And yes it is written as a binary file, 'tis how they decided to format the damn thing . The reads are correct according to the standard, unless I somehow misread it...

I think you meant the file was written with DataOutputStream, which, by definition, outputs something meant to be read in by DataInputStream. How exactly it does that depends on Sun's implementation.

you need to know exactly how Sun wrote the data out to the file, which means you need to remember what method you used to write it out, and then in C++ figure out how sun's data types differ from your platform's data types and make the conversion.

for example, writeBytes(String s) writes each character as one Byte discarding the high-order bits, whereas writeChars(String s) writeas each character as two Bytes. On my system, each character is saved in one Byte. Therefore, if I used writeChars(String s), I'd have to discard the upper 8 bits somehow (possibly with a double-read). I'd also have to make sure all the data was packed tight, with no empty spaces (because that's how fstream reads binary files)

the point is: DataOutputStream outputs data in a way that can be read by DataInputStream, but that doesn't necessarily mean it's a true binary file. Any spaces in a real binary file are data that the text editor is trying to display in an incompatible character set (ASCII).

Here's some code that writes and then reads a binary data file in C++: