• Advertisement
Sign in to follow this  

TGA loader

This topic is 4902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the problem with this function. The function is supposed to check if the file in m_File is a TGA-file. But it returns false. Anyone got any ide? The cout << str prints TRUEVISION-XFILE.
bool CTga::CheckIfTga()	
{
	char str[17];
	// Go to the TRUEVISION-XFILE start bit. 
	// And put the next 16 bits in str.
	m_File.seekg( -18, ios_base::end );	
	m_File.getline(&str[0], 17);	
	cout << str;
		
	// If the 16bits are "TRUEVISION-XFILE" then return true.
	if(str == "TRUEVISION-XFILE")
		return true;
	else
		return false;
}


Share this post


Link to post
Share on other sites
Advertisement
Very common mistake: do not compare C-style strings with ==. You're comparing pointers. Basically, what you've written translates to:


const char temp[] = "TRUEVISION-XFILE";
if (&(str [0]) == &(temp [0]))



Instead, either use C++ style strings or, if you stick to C:


if(!strcmp (str, "TRUEVISION-XFILE"))

Share this post


Link to post
Share on other sites
two things.
1) str == "whatever" will compare pointer addresses.
you will never get "true" out of that because str is located at
a different memory addresss.
Use if (0 == strncmp(str, "sig", sizeof(str)-1))

2) The footer extension is not manadantory, so a TGA might be valid
without it.

EDIT: damn, too slow [smile]

Share this post


Link to post
Share on other sites
Thanks for the help.

Kippesoep > What did you mean with C++ style strings? Do you mean the string class. But i couldn't use getline with that.

Share this post


Link to post
Share on other sites
When you use char buffers, you have to use strcmp() to compare strings. Operator== on char* just compares the pointer values, not what's stored in the pointed-at memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by zalthar
Kippesoep > What did you mean with C++ style strings? Do you mean the string class. But i couldn't use getline with that.


Sure you can:


string str;
str.resize (16);
...
//Note: I only read 16 bytes since a std::string doesn't need a trailing NUL
m_File.getline(&str[0], 16);
cout << str;



Or you can keep the char array and do the comparison like this:


if(string(str) == "TRUEVISION-XFILE")


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement