下一位代码将文件名作为字符串,如果文件是位图(扩展名为.bmp),则应该返回没有扩展名的名称。
obj.Fee
是一个字符串,其中包含输入文件夹的路径。
InputPicFolder
但我得到一个空字符串作为path p(InputPicFolder);
for (auto i = directory_iterator(p); i != directory_iterator(); i++)
{
if (!is_directory(i->path())) //eliminate directories
{
string filename = i->path().filename().string();
//string filename = "APicture.bmp";
int len = filename.length();
if (len > 4)
{
size_t found = filename.find('.');
string EXT = filename.substr(found + 1);
if (EXT.compare("bmp") == 0)
{
string filenameWOExtension = filename.substr(0, found);
cout << filenameWOExtension;
}
}
}
}
任何人都知道我做错了什么?我感觉这是一个非常愚蠢的问题,这是我疲惫的眼睛无法看到的。
edit - 使用boost部分更新代码。似乎没有改变结果。即使我用“APicture.bmp”覆盖字符串,我的输出仍然是filenameWOExtension
。
编译器 - MSVC14.1
答案 0 :(得分:0)
您的代码适合我:
function whatIsInAName(collection, source) {
var array = [];
var sourceNames = Object.getOwnPropertyNames(source);
// loop over each item in collection
for (var i = 0; i < collection.length; i++) {
var matchedProperties = 0;
// loop over each property in source
for (var j = 0; j < sourceNames.length; j++) {
// check whether the property is in collection[i] and
// whether the value matches
if (collection[i].hasOwnProperty(sourceNames[j]) &&
source[sourceNames[j]] === collection[i][sourceNames[j]]) {
matchedProperties += 1;
}
// add to array if all properties matched
if (matchedProperties === sourceNames.length) {
array.push(collection[i]);
}
}
}
return array
}
var result = whatIsInAName([{ a: 1, b: 2 }, { a: 1 }, { a: 1, b: 2, c: 2 }], { a: 1, b: 2 });
console.log(result);
我做的唯一改变就是如何获得源字符串。您是否有可能在您认为自己的字符串中搜索?
如果我的完整程序没有产生输出:#include <string>
#include <iostream>
using namespace std;
int main() {
string filename = "APicture.bmp"; // *** I Changed this
int len = filename.length();
if (len > 4)
{
size_t found = filename.find('.');
string EXT = filename.substr(found + 1);
if (EXT.compare("bmp") == 0)
{
string filenameWOExtension = filename.substr(0, found); //should be "APicture"
cout << filenameWOExtension;
}
}
}
,您应该提供有关编译器和运行时环境的更多信息。这是用std :: string接口编译的,但是它是一个真正的std :: string还是像某个一样的东西?
此外,为什么使用compare()而不是普通的==来比较2个字符串?