Thread: Reading a file in binary

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    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)

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    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

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( myfile.good() )
        cout << myfile.get() << " ";
    That should work. Check for eof, fail and bad.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    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

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I feel awful for missing that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 09:22 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Problem reading a delimited file into a binary tree
    By neolyn in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2004, 07:51 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM