-2

my font filename extractor is not working... filename_: ".\\Source\\AlexBrush-Regular.ttf" and I'm using

sprintf(buffer, "\nFont Name:%s \nNumber of errors: %d\nAscent %d High +%d Descent -%d Low -%d!", 
                font->filename_.substr( font->filename_.find_last_of('\\'), font->filename_.length() ), hitcount, Ascent, Descent, High, Low);

my ouput looks like buffer 0x008aecbc "\nFont Name:ÀëŠ \nNumber of errors: 12688232\nAscent -858993460 High +-858993460 Descent --858993460 Low -22!"

3 Answers3

1

Just use basename(font->filename_.c_str()) which does exactly what you want to do and is platform-independent.

mrks
  • 8,033
  • 1
  • 33
  • 62
1

I tried to reproduce your problem with a little example :

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string filename(".\\Source\\AlexBrush-Regular.ttf");

    cout << filename.substr(filename.find_last_of('\\') + 1) << endl;

    return 0;
}

and the output is :
AlexBrush-Regular.ttf

First, I add "1" to the first parameter, because the function find_last_of() will search the position of the last character and I don't think you want to include it in your output.
Secondly, I erase the second argument, because the default value is string::npos and it indicates all characters until the end of the string.

If you have always your problem, please be more specific to what you want.

1

It would be a better idea to solve this using C++ facilities (i.e. stringstream) rather than C facilities, considering you are using a std::string. In that case, please see this snippet here for how you might do that.

If you insist on using sprintf, then you will have to get the underlying c-string, which you can call like:

const std::string mystr("foo");
const char *mycstr(mystr.c_str());
bfoster
  • 79
  • 4