Search code examples
c++qtopencvprintfimread

sprintf - 4 useless ascii characters before string


I'm working with visual studio 10, qt addin and opecv library.

What I want to do is to load multiple files using a for-loop:

(I have ui.image_templates_comboBox->currentText() = "cat")

for (int i = 1; i <= 15; i++){
    string currentText = ui.image_templates_comboBox->currentText().toStdString();
    char name[40];
    sprintf(name, "Logos/cat/%s_%d.tif", &currentText, i);
    templ_img [i] = cv::imread( name );

So, I thought this should be working OK, but when I debug it, I hover my mouse above "name" and I notice that there are 4 non-english characters preceding currentText value.

I ask 2 questions:

a) How is it possible to ommit those 4 useless characters? (I typed them as "1234" as this site couldn't display them)

  • name 0x003a7b04 "Logos/cat/1234cat_1.tif" char [40]

b) It is possible to collapse those 4 lines into 1 using an expression inside imread()?


Solution

  • You are mixing frameworks to much and you do not understand how sprintf works. Fix it like that:

    for (int i = 1; i <= 15; i++){
        QString fileName = QString("Logos/cat/%1_%2.tif")
                           .arg(ui.image_templates_comboBox->currentText())
                           .arg(i);
        templ_img [i] = cv::imread(fileName.toAscii().data()); // or: toLocal8Bit, toLatin1(), toUtf8()