如何检查c中是否存在文件

foo*_*o_l 4 c linux filesystems

我有生成的核心文件,其pid附加到其名称或有时只有名称核心.我需要检查文件是否存在名称core.pidcore.我尝试使用stat()路径字符串的地方/tmp/core*,但失败了.能告诉我如何解决这个问题.谢谢你的时间.

Dag*_*ang 8

你可以使用这个access功能:

if (0 == access(path, 0)) { 
    file exists;
} 
else { 
    file does not exist;
}
Run Code Online (Sandbox Code Playgroud)

  • 访问的第二个参数的常量("0")被别名为"F_OK" (3认同)