C语言 C、检查文件是否存在而不能读/写可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13945341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:47:20  来源:igfitidea点击:

C, check if a file exists without being able to read/write possible?

cfopen

提问by Susan

Possible Duplicate:
What's the best way to check if a file exists in C? (cross platform)

可能的重复:
检查文件是否存在于 C 中的最佳方法是什么?(跨平台)

i would like to check if a file exists or not. I changed the permissions of my testfile to "chmod -r somefile". However now it says that the file does not exist even though it does exist.

我想检查一个文件是否存在。我将测试文件的权限更改为“chmod -r somefile”。但是现在它说该文件不存在,即使它确实存在。

So i assume if i dont have read permissions i cannot open the file with "fopen r". But that would mean that there is no easy way to tell if a file exists or cannot be read/written?

所以我假设如果我没有读取权限,我将无法使用“fopen r”打开文件。但这意味着没有简单的方法可以判断文件是否存在或无法读取/写入?

Or am i missing something? Any help would be great.

或者我错过了什么?任何帮助都会很棒。

Thanks!

谢谢!

int doesFileExist(const char* filename)
{
  FILE* fptr = fopen(filename, "r");
  if (fptr != NULL)
  {
    fclose(fptr);
    printf("File exists\n");
    return 1;
  }
  printf("File doesnt exist\n");
  return 0;
}

Update: Thanks guys for these great links and explanations!

更新:感谢这些伟大的链接和解释!

回答by Brian Campbell

fopenactually tries to open the file, which you can't do if you don't have read access. In order to check whether the file exists without opening it, use stat; statgives you metadata about the file, and requires only read access to the directory containing the file, not the file itself.

fopen实际上尝试打开文件,如果您没有读取权限,则无法打开该文件。为了在不打开文件的情况下检查文件是否存在,请使用stat; stat为您提供有关文件的元数据,并且只需要对包含文件的目录进行读取访问,而不需要对文件本身进行读取访问。

int doesFileExist(const char *filename) {
    struct stat st;
    int result = stat(filename, &st);
    return result == 0;
}

You could get fancier by checking errnoif resultis not 0; if errnois ENOENTthen the file does not exist, if it is ENOTDIRthen part of the path you provided is not a directory, if it's EACCESSthen you didn't have read permission on one of the directories in the path and so statcan't give you an answer, and so on.

你可以通过检查errnoifresult不是 0来变得更有趣;如果errno是,ENOENT则该文件不存在,如果是,ENOTDIR则您提供的路径的一部分不是目录,如果是,EACCESS则您对路径中的目录之一没有读取权限,因此stat无法给您回答等等。

Also, be aware that if you're on a platform with symlinks (any Unix-like, or Windows Vista or later), that you should be aware of whether you are querying about the symlink or the file it points to. If you call stat, then you are asking about the file it points to; if you have a symlink dir/linkwhich points to other/file, then statwill return results about other/file(which is usually what you want, since that's what you would get if you opened the file). However, if you are curious about the link itself (if you want to know "does dir/linkexist, even if other/filedoes not?"), then you should use lstat().

另外,请注意,如果您使用的是带有符号链接的平台(任何类 Unix 或 Windows Vista 或更高版本),您应该知道您是在查询符号链接还是它指向的文件。如果您调用stat,那么您是在询问它指向的文件;如果您有一个dir/link指向的符号链接other/filestat则将返回有关结果other/file(这通常是您想要的,因为如果您打开文件,这就是您将获得的结果)。但是,如果您对链接本身感到好奇(如果您想知道“是否dir/link存在,即使不存在other/file?”),那么您应该使用lstat().

stat()works on Windows as a compatibility wrapper (they prefer you use _stat(), and will warn if you don't), but it's generally better to use the native platform APIs. On Windows, you should probably use GetFileAttributes():

stat()在 Windows 上作为兼容性包装器工作(他们更喜欢您使用_stat(),如果您不使用,则会发出警告),但通常最好使用本机平台 API。在 Windows 上,您可能应该使用GetFileAttributes()

int doesFileExist(const char *filename) {
    return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES;
}

回答by paddy

You are not really checking whether the file exists here... You are checking if you can open it with read access. It could fail for reasons other than the file not existing. As you have seen, you might not have read permission. It could also be that the file is locked.

您并没有真正检查文件是否存在于此处...您正在检查是否可以使用读取访问权限打开它。它可能会因文件不存在以外的原因而失败。如您所见,您可能没有读取权限。也可能是文件被锁定了。

Check this answer:

检查这个答案:

What's the best way to check if a file exists in C? (cross platform)

检查文件是否存在于 C 中的最佳方法是什么?(跨平台)

It suggests using stat, which should work in most cases except if you don't have read access on the directory in which the file resides.

它建议使用stat,它应该在大多数情况下工作,除非您对文件所在的目录没有读取权限。

回答by Roman Dmitrienko

On Linux (and many other systems), you can use opendir()and friends to list directories. I guess read-only files will show up. There should be similar functions available on other platforms.

在 Linux(和许多其他系统)上,您可以使用opendir()和朋友来列出目录。我猜只读文件会出现。其他平台上应该也有类似的功能。