简体   繁体   English

std::filesystem::is_regular_file() vs::exists() - 什么算作常规文件

[英]std::filesystem::is_regular_file() vs ::exists()- what counts as a regular file

I am using Visual Studio 2017 C++ WITH MFC我正在使用 Visual Studio 2017 C++和 MFC


I have a program in MFC that collects filepaths as string s and adds them to a zip file.我在 MFC 中有一个程序,它将文件路径收集为string ,并将它们添加到zip文件中。

I want to add an error check, where I check if a file exists before trying to add it.我想添加一个错误检查,在尝试添加文件之前检查文件是否存在。 If it exists, perfect, I add the file to the zip .如果它存在,完美,我将文件添加到zip If not, not a problem, I continue to the next filepath.如果没有,没问题,我继续下一个文件路径。

I came across std::filesystem ( here ) and I see two different functions that I think can work: is_regular_file() ( here ) and exists() ( here ).我遇到了std::filesystem (这里),我看到了两个我认为可以工作的不同函数: is_regular_file() (这里) 和exists() (这里)。

However, I am not sure which of the two to use.但是,我不确定要使用两者中的哪一个。 The file types I will be zipping vary from .txt to .zip , and a lot of arbitrary file types.我将zipping的文件类型从.txt.zip以及许多任意文件类型。

From my research, they appear to be similar, both returning a bool value.根据我的研究,它们看起来很相似,都返回bool值。

What is the difference between the two functions and which is the better one to use?这两个功能有什么区别,哪个更好用?

Furthermore, from my understanding, the library is relatively new and might not be suitable for use in MFC .此外,据我了解,该库相对较新,可能不适合在MFC中使用。 Is this true?这是真的? And if so, what else could I use to check if given a file path, that file exists on the computer?如果是这样,我还能用什么来检查给定文件路径,该文件是否存在于计算机上?

Thank you in advance: :D提前谢谢你::D

tl;dr: use std::filesystem::is_regular_file() , std::filesystem::is_symlink() and std::filesystem::is_directory() . tl;博士:使用std::filesystem::is_regular_file()std::filesystem::is_symlink()std::filesystem::is_directory()


There are several file types defined in std::filesystem : std::filesystem 中定义了几种文件类型

Constant    Meaning
none        indicates that the file status has not been evaluated yet, or an error occurred when evaluating it
not_found   indicates that the file was not found (this is not considered an error)
regular     a regular file
directory   a directory
symlink     a symbolic link
block       a block special file
character   a character special file
fifo        a FIFO (also known as pipe) file
socket      a socket file
implementation-defined  an additional implementation-defined constant for each additional file type supported by the implementation (e.g. MSVC STL defines junction for NTFS junctions)
unknown     the file exists but its type could not be determined 

So in first glance, std::filesystem::exists() would be what you want.所以乍一看, std::filesystem::exists()就是你想要的。

But you're adding files to a ZIP archive, and AFAICT, ZIP only supports regular files, symlinks, and directories, so that's what you should use.但是您正在将文件添加到 ZIP 存档中,而 AFAICT、ZIP 仅支持常规文件、符号链接和目录,所以这就是您应该使用的。

You wouldn't archive sockets or NTFS junctions even if they existed and were given in command line.您不会归档 sockets 或 NTFS 连接,即使它们存在并在命令行中给出。


To clarify:澄清:

A "regular file" is a file that has data that is (a) stored by the filesystem and (b) has no semantics assigned to it by the filesystem - ie the filesystem doesn't know how to read the file or act on it. “常规文件”是具有 (a) 由文件系统存储的数据并且 (b) 没有由文件系统分配给它的语义的文件 - 即文件系统不知道如何读取文件或对其采取行动.

For example, a MS Word document is something that MS Word understand, but the filesystem only knows that it's a bunch of bytes.例如,MS Word 文档是 MS Word 可以理解的,但文件系统只知道它是一堆字节。
A symlink, on the other hand, is something the filesystem stores, and knows how to act on (follow the link).另一方面,符号链接是文件系统存储的东西,并且知道如何操作(按照链接)。
A block special file is something that is not actually stored by the filesystem, but rather just a piece of metadata that other parts of the OS act on.块特殊文件实际上并不由文件系统存储,而只是操作系统其他部分所作用的一段元数据。
A ZIP archive, or RAR, are files that the filesystem stores. ZIP 存档或 RAR 是文件系统存储的文件。 But it doesn't know how to read them or act on them, so they are regular files.但它不知道如何阅读它们或对它们采取行动,因此它们是常规文件。
In the old Windows Explorer, the "shortcuts" on your desktop were small files stored by the filesystem, but it was Explorer that knew how to read them - they weren't symbolic links, they were regular files.在旧的 Windows 资源管理器中,桌面上的“快捷方式”是文件系统存储的小文件,但知道如何读取它们的是资源管理器——它们不是符号链接,它们是常规文件。 (Maybe that's still the case, I haven't checked recently.) (也许还是这样,我最近没查过。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM