Sign in to follow this  
zalthar

TGA loader

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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this