我正在运行以下程序,该程序使用stat()检查文件是否存在。但是,当我传递一个路径时,比如$HOME/file.sh,它会失败并返回ENOENT错误。但是当我传递扩展路径,即/root/file.sh时,stat()返回成功,即退出代码0。
int main ()
{
struct stat statbuf;
char path [1024];
strcpy(path,"$HOME/file.sh");
int rc = stat(path,&statbuf);
if (rc == -1 )
{
printf ("File not found !!\n");
}
else
printf("Found it !!\n");
return 0;
}发布于 2012-09-18 21:24:48
strcpy()不会将环境变量$HOME展开为它的值,但会复制指定的提取字符串文字。您可以使用getenv()获取$HOME的值。
将失败消息更改为:
printf("File not found: %s\n", path);以供确认。
https://stackoverflow.com/questions/12477936
复制相似问题