- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在阅读stat
方法的手册here它说:
Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.
To check if a file exists without manipulating it afterwards, fs.access() is recommended.
所以,我有两个问题:
为什么使用错误处理程序比 fs.stat()
更适合检查文件是否存在?
并且由于我可以使用 fs.access()
检查文件是否存在,使用 error handler
机制仍然是确保文件打开的首选方法吗?
我想我找到了第二个问题的答案:
Using fs.access() to check for the accessibility of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.
所以 fs.open()
可能会阻塞其他进程的文件,而 fs.stat()
和 fs.access()
只是请求信息和其他进程仍然可以更改/删除文件。
最佳答案
我认为这里应该明确的是 fs.stat
和 fs.access
都不推荐用于特殊情况在打开文件之前检查文件的可访问性。正如问题中提到的那样,这可能会触发竞争条件。函数 exists()
和 existsSync()
由于这个原因(以及其他一些与 API 相关的原因)被弃用(大约版本 4):它们经常被利用目的。
当试图打开文件时,如果文件不可访问,该操作将触发错误。因此,此类检查应在此处处理。否则,有 more than one reasonable way to check if a file exists .
另请注意,从版本 6.8.0 开始,existsSync()
已被弃用!参见 discussion和 6.8.0 changelog .上述规则同样适用:只有在您之后不打算打开文件时才使用它。
关于javascript - 为什么不推荐使用 fstat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40259928/
我试图阅读 fstat 的手册页,我得到了 fstat(2) , fstat(3)和 fstat(3P) . 过去使用过 system (2) 命令,我知道不同之处在于我必须自己编写一个原型(prot
我正在尝试读取这两个文件的大小以确定两个文件中哪个较小,但第二个文件的结果总是为零,而第一个文件的大小甚至都不正确,有什么想法吗? #include #include #include #inc
如果您只需要文件大小和创建日期,是否有使用 fstat() 函数的替代方法? 我知道我们可以使用 ftell() 来获取文件大小,但我也想要创建时间和日期。我不想使用 fstat() 的原因是如果文件
文件在那里,里面有 json 数据。我想知道文件的长度。但是当我尝试下面的代码时,它的大小仍然为 0。 int file_contentl_Len = 0; int fd_0 ; fd_0 = ope
我正在阅读stat 方法的手册here它说: Using fs.stat() to check for the existence of a file before calling fs.open()
如何获取系统中文件描述符的当前计数? 我知道如何获得最大值。 % sysctl kern.maxfiles kern.maxfiles: 8232 引用: http://www.freebsd.org
我的代码比较 2 个文件大小,其行为似乎类似于 keyfile > sourcefile。下面的代码是否缺少某些内容以及我可以更改什么?我用于测试的两个文件是 3kb key 文件和 14kb 源文件
我正在尝试查看哪个文件最近被修改过。如何比较找到的日期以找到最近的日期? localtime_r(&file_info.st_mtime), &time); asctime_r(&time, asct
所以,我有一个作业,基本上是一个比较系统调用速度和库函数速度的练习。我们正在对通过标准输入从文件中获取的字符串进行排序。我们应该使用 fstat 来确定该文件是否为常规文件。我已经通读了手册页,虽然我
在许多情况下,我看到在调用 open 分配文件描述符后直接执行 fstat 调用: fd = open(file, flags, mode); fstat_result = fstat(fd, &st
所以我对 C 很陌生。我用 Java 做过很多编程,但发现学习 C 非常困难。 目前,我被分配从我们的终端窗口读取一个文件,其中将包含一个整数列表。我们必须从这个列表中读取值并计算平均值,我相信我做对
我考虑读取未知大小的文件,我知道它不会同时改变大小。所以我打算使用fstat()函数和struct stat。现在我正在考虑 st_size 字段的真正含义以及我应该如何使用它。 如果我以这种方式获得
#include #include #include #include int main(int argc, char const *argv[]) { struct stat buf
#include #include #include #include int main(int argc, char const *argv[]) { struct stat buf
我正在 Raspbian 上编译 Angband 变体,但出现以下错误: struct stat txt_stat, raw_stat; /* Build the filename */ path_b
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在做一个fstat在我的文件描述符上并将其转储到 struct stat 。我阅读了 fstat 的文档(链接如下),它声称有成员 st_atime和st_mtime . http://pubs.
我正在尝试从 fstat() 获取设备的次要和主要编号。对 fstat() 的调用是在预加载的 mmap() 系统调用中完成的: // preload.so // compile with: // g
我使用 fstat 来获取文件大小。我想使用这个大小来声明一个数组,然后用另一个 fstat 更改大小并重新使用同一个数组。示例: fstat(file1, &fileStat); fsize = f
我目前正在使用沙盒 PyPy 编写 Python 沙盒。基本上,沙箱通过提供一个“ Controller ”来工作,该 Controller 将系统库调用映射到指定的函数。按照 codespeak 中
我是一名优秀的程序员,十分优秀!