这个帖子基础上给出here的解决方案,后在SO从字符串转换为char * - 传递函数指针
我写一个函数来获取给定的文件扩展名(string类型),是这样的:
void GetFileExtension(string filename, char* extension)
{
vector<string> rec;
StrDelimit(filename, '.', rec);
//cout << rec[rec.size()-2].c_str()[1] << endl;
//extension = rec[rec.size()-2].c_str()[0];
//extension = &rec[rec.size()-2].c_str()[0];
string str = rec[rec.size()-2];
// conversion from string to char*
vector<char> writable(str.size() + 1);
std::copy(str.begin(), str.end(), writable.begin());
//cout << writable << endl;
extension = &writable[0];
}
StrDelimit正常工作已经接收的字符串和由给定的定界符
我使用了“//从字符串转换为char *”,如图到结果返回到调用主程序界定于子串的矢量
没有编译错误,但结果是一串无意义的字符。这是什么问题?
任何帮助将不胜感激!
谢谢!
谢谢你的完整答案:) – 2013-02-24 14:51:35