Inserting string in another string at specific index c++/Qt -
i have qstring (it may not matter since convet std::string anyway) contains full unix path file. e.g. /home/user/folder/name.of.another_file.txt.
i want append string file name right before extension name. e.g. pass function "positive", here call vector_name , @ end want /home/user/folder/name.of.another_file_positive.txt (note last underscore should added function itself.
here code not compile, unfortunately!!
qstring temp(mpath); //copy value of member variable doesnt change??? std::string fullpath = temp.tostdstring(); std::size_t lastindex = std::string::find_last_of(fullpath, "."); std::string::insert(lastindex, fullpath.append("_" + vector_name.tostdstring())); std::cout << fullpath; i following errors:
error: cannot call member function 'std::basic_string<_chart, _traits, _alloc>::size_type std::basic_string<_chart, _traits, _alloc>::find_last_of(const std::basic_string<_chart, _traits, _alloc>&, std::basic_string<_chart, _traits, _alloc>::size_type) const [with _chart = char; _traits = std::char_traits<char>; _alloc = std::allocator<char>; std::basic_string<_chart, _traits, _alloc>::size_type = long unsigned int]' without object std::size_t lastindex = std::string::find_last_of(fullpath, "."); cannot call member function 'std::basic_string<_chart, _traits, _alloc>& std::basic_string<_chart, _traits, _alloc>::insert(std::basic_string<_chart, _traits, _alloc>::size_type, const std::basic_string<_chart, _traits, _alloc>&) [with _chart = char; _traits = std::char_traits<char>; _alloc = std::allocator<char>; std::basic_string<_chart, _traits, _alloc>::size_type = long unsigned int]' without object std::string::insert(lastindex, fullpath.append("_" + vector_name.tostdstring())); p.s. appriciate if can tell me how can achieve using qstring or qt library itself!
the reason error find_last_of , insert member functions of std::string, not static or non-member functions. therefore need object access function.
the corrections below:
std::size_t lastindex = fullpath.find_last_of("."); fullpath.insert(lastindex, "_" + vector_name.tostdstring()); cout << fullpath; also, may want test return values of function calls. if there no "." in file name?
Comments
Post a Comment