【发布时间】:2012-10-22 16:52:04
【问题描述】:
在 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;
}
【问题讨论】:
-
请发布您正在使用的
stat()电话。 -
并且至少使用
perror通知用户任何失败的系统调用。 -
准确地说,调用失败时
strFilename.c_str()的值是多少?您可以通过将perror(strFilename.c_str())放在else语句中来发现这一点。