0

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()?

AchiPapakon
  • 328
  • 5
  • 19
  • 1
    Given the answers already posted, you can guarantee that all of that information fits in a character array of only 40 bytes? At the very least, make the buffer larger, and not dancing on the knife's edge of a buffer overflow error. – PaulMcKenzie Jan 02 '15 at 14:48

2 Answers2

5

You cannot use the address of an std::string where a const char* is expected. They are not the same.

sprintf(name, "Logos/cat/%s_%d.tif", currentText.c_str(), i);
nvoigt
  • 75,013
  • 26
  • 93
  • 142
2

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()
Marek R
  • 32,568
  • 6
  • 55
  • 140