在 Linux 12.04 上,我有一个可执行文件位于:
/a/b/exe
和一个配置文件
/a/b/config
做的时候:
cd /a/b/
./exe
一切正常,stat 函数在 /a/b/ 上找到文件配置
但是,从 root 运行时
/a/b/exe
stat 找不到配置文件
知道为什么吗?
它使得无法使用不是从 exe 文件夹中运行的脚本来运行二进制文件。
编辑
调用如下所示:
struct stat stFileInfo;
bool blnReturn;
int intStat;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0) {
// We were able to get the file attributes
// so the file obviously exists.
blnReturn = true;
} else {
// We were not able to get the file attributes.
// This may mean that we don't have permission to
// access the folder which contains this file. If you
// need to do that level of checking, lookup the
// return values of stat which will give you
// more details on why stat failed.
blnReturn = false;
}