What should a student do if a professor agrees to write recommendation, then retracts offer upon realizing he confused the student with someone else?
The questions-not-asked include asking about the basis for student B's actions. As @BenBitdiddle comments after his answer, perhaps student B was "advised" to ask the most famous professor, etc.

My blanket reaction to this is that this advice should not have come from any professionally competent "advisor", but probably from peers, or peer-based information. Abstractly, there's certainly nothing wrong with getting the opinions of one's friends and peers. However, it is exactly in such situations that the critical weakness in asking people who don't know much about a given thing is instantiated. How to know that this would be a problem? Difficult, for many understandable reasons, but that does not resolve the issue! 

That is, younger people would benefit from appreciating the palpable fact that important decisions affecting their lives are made by people (often older) who need not share their viewpoint, nor their same-age friends' viewpoint.

While it is true that some people who "have power" abuse it, this is not a universal, and it is not wise to postulate that all experienced/older people are oppressive or selfish. Genuine experience, as opposed to conjecture, is hard to replace. Thus, ideally, letter-of-recommendation writers are sufficiently experienced to have been through (and succeeded in) the endeavor for which they're writing a letter. Perhaps also experience (and successful in) appraising the likely future success of people in those endeavors. From this comes the value of letters of recommendation.

Yes, unfortunately, even a pretty-darn-good performance in a routine course doesn't give a letter writer much to work with. Many people hit that mark, etc. 

What's a student to do who has trashed all those bridges, before they realized that it'd matter? Probably spend extra time proving exactly that they'd caught on, and have moved to a different plateau. But there'd need to be tangible evidence, not just a promise.

So, in effect, "tell your friends not to hope for letters from faculty in whose courses they'd done badly"... And, if people discover themselves in the position that there's no alternative, then they need to get as close to a "do-over" as they can, because otherwise they've sealed their own fate, in any conventional avenue.

Yes, it is indeed unfortunate that this "appraisal" period comes during a period of peoples' lives where many things are in turmoil... 

So, again, people who become aware of this mechanism should "tell their friends" to work hard to avoid finding themselves in such a situation. If one does, then it is almost surely better to allocate considerable time to repair the damage, rather than somehow "fake it" and limp along with fatally bad letters of recommendation.

By the way, part of the "currency" that faculty have to spend (or not) is their word/reputation, so they are very, very hesitant to blow it on bogus not-so-bad recommendations. Faculty who'll give glowing recommendations to nearly anyone will have debased that currency to an extent that it is nearly worthless... so you'd not gain from a letter from them anyway.