我遇到了一个奇怪的字符串大小问题。这是我的示例代码
样品A
std::string getexepath()
{
char result[ PATH_MAX ];
int found;
ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
found = std::string(result).find_last_of("/");
std::string pp = std::string(result).substr(found+1);
std::string kk = std::string(result).substr(found+1);
std::cout << kk << kk.size() << std::endl;
return kk;
}
样品B
std::string getexepath()
{
char result[ PATH_MAX ];
int found;
ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
found = std::string(result).find_last_of("/");
//std::string pp = std::string(result).substr(found+1);
std::string kk = std::string(result).substr(found+1);
std::cout << kk << kk.size() << std::endl;
return kk;
}
输出不同;sampleB 输出比 sampleA 多 2 个字符。我想区别是'\0'
; sampleB 输出包含'\0'
,而 sampleA 不包含。
我想知道是什么导致了这个问题。在查看答案后,现在我知道我错过了“\0”。但我仍然想知道为什么添加这个语句“std::string pp = std::string(result).substr(found+1)”会导致不同的结果。
谢谢