我正在使用 visual studio 10、qt 插件和 opecv 库。
我想做的是使用 for 循环加载多个文件:
(我有 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", ¤tText, i);
templ_img [i] = cv::imread( name );
所以,我认为这应该可以正常工作,但是当我调试它时,我将鼠标悬停在“名称”上方,我注意到在 currentText 值之前有 4 个非英语字符。
我问了两个问题:
a) 怎么可能省略那 4 个无用的字符? (我将它们输入为“1234”,因为该站点无法显示它们)
- 名称 0x003a7b04 "Logos/cat/1234cat_1.tif"字符 [40]
b) 可以使用 imread() 中的表达式将这 4 行合并为 1 行吗?
最佳答案
您不能在需要 const char*
的地方使用 std::string
的地址。它们不一样。
sprintf(name, "Logos/cat/%s_%d.tif", currentText.c_str(), i);
关于c++ - sprintf - 字符串前有 4 个无用的 ascii 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27743587/