为什么我不能使用FOPEN? [英] Why can't I use fopen?

查看:206
本文介绍了为什么我不能使用FOPEN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个previous问题,我问了一下所谓的安全的库德precations 中,我发现自己同样感到困惑,为什么 fopen()函数应该是去precated。

In the mold of a previous question I asked about the so-called safe library deprecations, I find myself similarly bemused as to why fopen() should be deprecated.

这个函数有两个C字符串,并返回一个FILE * PTR,如果失败,NULL。在哪里线程安全问题/串溢出的问题?或者是别的什么?

The function takes two C strings, and returns a FILE* ptr, or NULL on failure. Where are the thread-safety problems / string overrun problems? Or is it something else?

在此先感谢

推荐答案

有一个正式的ISO / IEC JTC1 / SC22 / WG14(C语言)技术报告 TR24731-1 (边界检查接口)其理由可在:

There is an official ISO/IEC JTC1/SC22/WG14 (C Language) technical report TR24731-1 (bounds checking interfaces) and its rationale available at:

  • http://www.open-std.org/jtc1/sc22/wg14

也有走向TR24731-2工作(动态分配功能)。

There is also work towards TR24731-2 (dynamic allocation functions).

有关规定的理由 fopen_s()是:

在创建文件,在 fopen_s freopen_s 功能通过设置防止未经授权访问的文件提高安全性它的文件保护和独占访问打开文件。

6.5.2 File access functions

When creating a file, the fopen_s and freopen_s functions improve security by protecting the file from unauthorized access by setting its file protection and opening the file with exclusive access.

该规范说:

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
errno_t fopen_s(FILE * restrict * restrict streamptr,
                const char * restrict filename,
                const char * restrict mode);

运行约束

streamptr 文件名,或无模式应是一个空指针。

Runtime-constraints

None of streamptr, filename, or mode shall be a null pointer.

如果有一个运行时约束冲突, fopen_s 不会试图打开一个文件。
此外,如果 streamptr 不是空指针, fopen_s * streamptr 来的
空指针。

If there is a runtime-constraint violation, fopen_s does not attempt to open a file. Furthermore, if streamptr is not a null pointer, fopen_s sets *streamptr to the null pointer.

fopen_s 函数打开,其名称是指向的字符串文件
  文件名,和流与它关联。

The fopen_s function opens the file whose name is the string pointed to by filename, and associates a stream with it.

应为的fopen 描述的模式字符串与模式开始加入
  以字符'W'或'A'可以用字符Upceded $ P $,见下图:

The mode string shall be as described for fopen, with the addition that modes starting with the character ’w’ or ’a’ may be preceded by the character ’u’, see below:


      
  • UW 截为零或建立文字的文本文件,默认权限

  •   
  • UA 追加;打开或在结束文件,默认权限
  • 创建写入文本文件
      
  • UWB 截为零或写创建的二进制文件,默认权限

  •   
  • UAB 追加;打开或在结束文件,缺省写入创建的二进制文件
      权限

  •   
  • UW + 截为零或创建更新的文本文件,默认权限

  •   
  • UA + 追加;打开或创建更新的文本文件,在结束文件,缺省写入
      权限

  •   
  • UW + B UWB + 截为零或更新创建二进制文件,默认
      权限

  •   
  • UA + B UAB + 追加;打开或创建更新的二进制文件,在文件结束的写作,
      默认权限

  •   
  • uw truncate to zero length or create text file for writing, default permissions
  • ua append; open or create text file for writing at end-of-file, default permissions
  • uwb truncate to zero length or create binary file for writing, default permissions
  • uab append; open or create binary file for writing at end-of-file, default permissions
  • uw+ truncate to zero length or create text file for update, default permissions
  • ua+ append; open or create text file for update, writing at end-of-file, default permissions
  • uw+b or uwb+ truncate to zero length or create binary file for update, default permissions
  • ua+b or uab+ append; open or create binary file for update, writing at end-of-file, default permissions

要底层系统支持的概念的情况下,打开的文件写入
  应独家开(也称为非共享)访问。如果文件正在被
  创建的,并且模式字符串的第一个字符不是U,在某种程度上,该
  底层系统支持的话,该文件将有$另一个p $ pvents文件权限
  系统上的用户访问该文件。如果正在创建的文件,第一个字符
  模式的串是'U',然后由文件已被关闭时,它应具有
  系统默认的文件访问权限 10)

To the extent that the underlying system supports the concepts, files opened for writing shall be opened with exclusive (also known as non-shared) access. If the file is being created, and the first character of the mode string is not ’u’, to the extent that the underlying system supports it, the file shall have a file permission that prevents other users on the system from accessing the file. If the file is being created and first character of the mode string is ’u’, then by the time the file has been closed, it shall have the system default file access permissions10).

如果文件被成功打开,然后将指针文件 streamptr 指出,
  将被设置为指针的对象控制打开的文件。否则,该指针
  到文件 streamptr 指出,将被设置为一个空指针。

If the file was opened successfully, then the pointer to FILE pointed to by streamptr will be set to the pointer to the object controlling the opened file. Otherwise, the pointer to FILE pointed to by streamptr will be set to a null pointer.

返回

fopen_s 函数返回零,如果它打开的文件。如果没有打开文件,或者如果
  有一个运行时约束冲突, fopen_s 返回一个非零值。

The fopen_s function returns zero if it opened the file. If it did not open the file or if there was a runtime-constraint violation, fopen_s returns a non-zero value.

10)这是该文件将被使用和fopen创建相同的权限。

10) These are the same permissions that the file would have been created with by fopen.

这篇关于为什么我不能使用FOPEN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆