Jump to content
  • 0

Struggling with simple C++ string loop


antz

Question

I'm trying to get the following code to work, but i'm falling foul on the line

   printf("Generated %s.sql\\n",filename);

Remove this line and it runs fine, with it crashes when its empty.

I've also tried wrapping this in:-

if(!filename.empty())

Here's the main segment of code:-

   // extract DBCs
   int count = 0;
   for (std::set<std::string>::iterator iter = dbcfiles.begin(); iter != dbcfiles.end(); iter++)
   {
       std::string filename = path;
       filename += (iter->c_str() + strlen("DBFilesClient\\\\"));

       if (ExtractFile(iter->c_str(), filename))
       {
           //Generate SQL files Here
           if (CONF_generate_sql_files)
           {
   printf("Generated %s.sql\\n",filename);
           }
       }
           ++count;
   }

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

Your problem is the format specifier used in printf. The %s in it tells the function that the argument is expected to be a C string, ie something of type 'const char*'. But it is not, since you pass it a C++ string, and that causes havoc as you've noticed.

Fortunately, std::string has a conversion to a C string (but you know since you use it in other place). So just change the code like this and that part will work:

 printf("Generated %s.sql\\n",filename.c_str());

Link to comment
Share on other sites

compared to some, my dev language progression is badly wrong :D

BASIC > VB3 > VB6 > Java > ASP > VB.Net > ASP.Net > C# and now C++

Not really, from my point of view is from easiest to hardest so it's a good learning curve.

OFF: Speaking of printf, why did MaNGOS choose C style deprecated error-prone output and <cstdio> instead of C++'s clean, more error-safe cout and <iostream>?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use