-
Reading a file in binary
Hi guys,
I'm trying to read in a file in binary mode. The file is 684 bytes. If you look at it under a hex editor, you'll notice that there are 15 bytes of all zeros starting around the 85th byte. For some reason, whenever I try to read this, the buffer will store everything up to the end of those 15 bytes and then doesn't store anything else from the file.
Does anyone know why this might be happening? It's a bit mind-boggling to me.
Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
int main()
{
ifstream myfile("STLBox.stl", ios::binary || ios::in);
char bigbuffer[684];
myfile.read(bigbuffer, 684);
cout << bigbuffer;
}
I've attached the file as well (you'll have to rename it to STLBox.stl)
-
The reading of data is working correctly. The printing it out afterward is what's wrong. I'm not sure why you would expect to output a string and somehow have it ignore the null terminator (which you already know is in the stream)
-
Sorry, that's just in there from earlier testing... I looked at the character array in the watchlist of Visual Studio, and it only contains default (garbage) characters after the 15 bytes (chars) of 0's.
Earlier, I had been trying to read in the file with a for loop, inputting 4 bytes at a time into a char array, yet it would crap out consistently at 102 (total) bytes read... So, I attempted doing it this way in order to simply read the whole file at once.
Can you verify it's reading the entire file for you? If it isn't, do you know what might be causing this?
Thanks!
-Max
-
Shouldn't you be using unsigned characters?
Code:
#include <fstream>
#include <iostream> //why do you have this and fstream?
#include <cstdlib>
int main()
{
ifstream myfile("STLBox.stl", ios::binary || ios::in);
unsigned char bigbuffer[684];
for( size_t x = 0; x < 600; x++ )
{
myfile.read(bigbuffer, 1);
cout << (int) bigbuffer[ x ]; << " ";
}
return 0;
}
Can't you just do something like that?
Quzah.
-
Read takes a character pointer, not an unsigned character pointer. So to do it that way, I tried:
Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream myfile("STLBox.stl", ios::binary || ios::in);
unsigned char bigbuffer[684];
for (size_t x = 0; x < 150; x++)
{
myfile.read((char *)bigbuffer, 1);
cout << (int) bigbuffer[0] << " ";
}
}
Bear in mind, that read call only puts the one byte into the first spot of the character array (bigbuffer[0]).
I took it down to 150 just to see the output. If you run it, you'll see that it "hangs" on the value of 0x76 (which occurs in the 100th byte)... The only possible thing I could think of is the next value is 0x1A, which is a "substitute" character, according to the ascii table... Still, that shouldn't effect the price of tea in China, should it? :P
-
Code:
while( myfile.good() )
cout << myfile.get() << " ";
That should work. Check for eof, fail and bad.
Quzah.
-
Madre de Dios!
...
Note to self: "|" != "||"...
The line "ifstream myfile("STLBox.stl". ios::in || ios::binary);" had an extra "|". So, it was opening in character mode. Doing it correctly, with ios:: in | ios::binary works as advertised.
Moral of the story: it's always something mundane and stupid ;)
Thanks for your help, guys :)
-
I feel awful for missing that.