Skip to main content
gamedev.net
🔒 Locked

Buffer problem

Started by Austrian Coder May 30, 2003 at 6:11 AM 1 replies 500+ views
Original Post
Austrian Coder
Austrian Coder
Hi! I have this here:
char* buffer = 0;

// search file in array

for (int i = 0; i < m_files.size(); ++i)
{
	if (!strcmp(m_files[i].Name, Filename))
	{
		// file found in archiv

		// resize buffer

		unsigned int size = m_files[i].Size;
		buffer = (char*) malloc (size);
		// go to dataoffset

		fseek (m_pFile, m_files[i].DataOffset, SEEK_SET);
		// read out of file

		fread (buffer, 1, size, m_pFile);

		// only for debuging

		int a = strlen(buffer);
		if (a) a = 0;
	}
}		
If i run this peace of code with size = 22 then in the end a = 40, but it should also be 22. The buffer contains the correct data, but the last 18 byte there is nonsene. Could somebody help me? Thanks, Christian
Estor
Estor
Probobly after read there is no null in readed buffer. So alloc
one byte more:
buffer = (char*) malloc (size+1);
then
memset (buffer, 0, size+1);
or
buffer[size] = 0;

and it sould work
"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We WouldWhen Losers Say Its Over With You Know That It's A Lie The Gods Made Heavy Metal And It's Never Gonna Die"THE GODS MADE HEAVY METAL/by ManOwaR
Austrian Coder
Austrian Coder
buffer = (char*) malloc (size + 1);
memset (buffer, 0, size + 1);

works perfectly!
Thanks.

[edited by - Austrian Coder on May 30, 2003 7:40:00 AM]

Topic Locked

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

Sign in to reply to this topic.