Jump to content
  • Advertisement
Sign in to follow this  
Phoresis

C++ Problem with strings

This topic is 4169 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

I'm having some trouble with strings, wchar arrays and the like. Here is part of the code:
		char *pmbbuf   = (char *)malloc( 100 );
		wcstombs( pmbbuf, FindFileData.cFileName, 100 );
		string test = pmbbuf;

		if (test.find(".bmp",0))
		{
			_tprintf (TEXT("First file name is: %s\n"),
				FindFileData.cFileName);
		}

The problem is it seems to be executing the if statement no matter what. I want the if statement to only be executed if the string ends in ".bmp" Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
what is wcstoms() doing?
and what is FindFileData?
are you taking the name from a "bmp" file?

a little explanation of what is actually going on would be helpful.

Share this post


Link to post
Share on other sites
sorry, my bad, I thought these things were used often.

FindFileData.cFileName is an array of wchar returned by a call to FindFirstFile:

hFind = FindFirstFile(DirSpec, &FindFileData);

wcstombs turns an array of wchar to an array of char.

I then assign the array of char to a string so that I can perform a find on it.

Share this post


Link to post
Share on other sites
string::find will return the index of the first occurrence of the searched-for string, or string::npos otherwise.

Your test will fail only if the string begins with ".bmp". Is that what you want?

--smw

Share this post


Link to post
Share on other sites
ah no. damn, I must have misread what find does. What i'm trying to achieve is check whether the string ends with .bmp (or at the lest check if the string contains ".bmp").

Share this post


Link to post
Share on other sites
std::string::find returns std::string::npos if it can't find something, which is typically a large unsigned number (remember std::string::find retursn the index it found the string at, so 0 is a valid return if it is the start of the string).

Also, consier using an automatic buffer of fixed size rather than a heap buffer, its easier to manage:

char pmbbuf[100];

// no need to free
// free(pmbbuf);


You could use a std::vector<char> too, but if its all local to one function then a raw char array will be pretty much the same.

[edit: lateness]

Share this post


Link to post
Share on other sites
Quote:
Original post by Phoresis
So :

size_t length = test.length();

if (test.find(".bmp",0) < length)

Should work ok.


I would be wary about integral signedness and strict weak ordering. Idiomatically, it's like this.

if (test.find(".bmp") != string::npos)
{
// go wild
}


--smw

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma

if (test.find(".bmp") != string::npos)
{
// go wild
}



...is correct.

Share this post


Link to post
Share on other sites
Not really. What if I give you "foo.bmp.tgz"? :)

You can try starting the search at position .size() - 4, or:


bool endsWith(const std::string& s, const std::string& suffix) {
return s.size() >= suffix.size() &&
std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
}

if (endsWith(test, ".bmp")) {
// go nuts
}

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!