Skip to main content
gamedev.net
🔒 Locked

Problem reading binary file

Started by Austrian Coder 7 replies 500+ views
Original Post
Austrian Coder
Austrian Coder
Here is my source, to read an binary file completly in an buffer.
		FILE*			pfile;
		unsigned int	Size;
		char*			buffer;

		if ((pfile = fopen(File, "rb")) != 0)
		{
			 // obtain file size.

			fseek (pfile , 0 , SEEK_END);
			Size = ftell (pfile);
			rewind (pfile);
		
			// allocate a buffer, which is one byte

			// biger, so that at the end of the

			// buffer is the needed 0.		

			buffer = (char*) malloc (Size + 1);
			memset (buffer, 0, Size + 1); 

			fread (buffer, 1, Size, pfile);

			// check if reading was ok

			if (!buffer)
			{
				cout << "Error while reading file!" << endl;
				return false;
			}

			// close file

			fclose(pfile);
		}
		else
		{
			// we could not open the file

			cout << "Could not open file" << endl;
			return false;
		}
If i want to load now a bmp completly into a buffer, the buffer is only 4 elements big. Whats wrong? Thanks, Christian
Fruny
Fruny
quote:

// allocate a buffer, which is one byte
// biger, so that at the end of the
// buffer is the needed 0.



What needed zero are you talking about ? This is not a C string.
Additionally, there are few cases where malloc is useful in C++.
This is not one of them.


#include <fstream> // for std::ifstream

#include <vector> // for std::vector

#include <iterator> // for std::istreambuf_iterator

#include <algorithm> // for std::copy


using namespace std; // So that you can omit the std:: prefix


// open the file

ifstream ifs( File, ios::binary );

// declare the buffer

vector<chat> buffer;

// read the file

buffer.assign( istreambuf_iterator<char>( ifs ),
istreambuf_iterator<char>() );

// close the file (automatic when ifs gets destroyed)

ifs.close()

// If *really* necessary, copy it into a char* buffer

char* cbuf = new char[buffer.size()];
copy( buffer.begin(), buffer.end(), cbuf );

// sometime later, don't forget to do

delete[] cbuf;



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on July 16, 2003 6:58:09 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
T2k
T2k
quote:
if (!buffer)

this will only fail if ur malloc (mem allocation) fails, not if u cant read the file, place it directly after malloc to prevent writing into an invalid buffer

quote:
Size + 1

why +1 ??? do u check 4 nulltermination? if so dont do it!

... ur code looks ok, reads in the file into the buffer, u dont need to set memory before reading (memset) and ur buffer has the size of ''Size + 1''

but u maybe need to understand how a bmp is build, see BMP Fileformat


T2k
JY
JY
What makes you say it''s only 4 elements long?

Try:

Checking the value of size
Checking the return value of fread
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
Fruny
Fruny
quote:
Original post by JY
What makes you say it''s only 4 elements long?



I bet he''s taking the size of the pointer for the size of the array.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Austrian Coder
Austrian Coder
Ok... i have made some changes:


FILE* pfile;
unsigned int Size;
char* buffer;

// no we try to open the file

// try to open 2 source files

if ((pfile = fopen(File, "rb")) != 0)
{
// obtain file size.

fseek (pfile , 0 , SEEK_END);
Size = ftell (pfile);
rewind (pfile);

// allocate a buffer, which is one byte

buffer = new char[Size];

fread (buffer, 1, Size, pfile);

// check if reading was ok

if (!buffer)
{
cout << "Error while reading file!" << endl;
return false;
}

// close file

fclose(pfile);
}
else
{
// we could not open the file

cout << "Could not open file" << endl;
return false;
}
...
delete[] buffer;



If i now make a debug breakpoint at if (!buffer) then in msvc 6.0 it shows me this: BM` and two black quads. Thats all... I need the whole file in a buffer, to compress it and save it again.


[edited by - Austrian Coder on July 16, 2003 7:20:18 AM]

[edited by - Austrian Coder on July 16, 2003 7:21:00 AM]
JY
JY
quote:
I bet he''s taking the size of the pointer for the size of the array.


Yip, that would be my guess. If you are using Visual Studio, it looks at char * buffers as text strings so stops displaying them once it gets to a \0 character. A bitmap is a binary file which can quite possibly contain \0 characters.

Try casting buffer to a BITMAPFILEHEADER structure pointer and check the members. You should then see at least the size of the file by looking at the bdSize member.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
Yanroy
Yanroy
I''m not sure if it will make a difference or not, but I would declare buffer as type BYTE. Because, after all, it''s not a character array. Although BYTE = unsigned char, so maybe that''s not any better

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.