在 C 语言中使用 open() 创建文件

2024-04-16

这是我第一次使用open() from:

#include <fcntl.h>

我正在尝试创建两个文件:

int fd;
int fd2;
char *tmpname = "./TMPFILE";
printf( "Temporary file created\n ");
char *tmpname2 = "./TMPFILE2";
printf( "Temporary file two created\n ");
fd = open(tmpname, O_WRONLY | O_APPEND);
fd2 = open(tmpname2, O_WRONLY | O_APPEND);

我正在尝试在当前工作目录中创建可以写入和附加的文件。

它可以编译并运行,但我担心的是,当我检查目录以查看文件是否已创建时,它们没有列出。

我的问题是open()只创建临时文件,这些文件在程序运行后会被删除,还是我搞砸了?


创建文件时,需要第三个参数来打开(mode)。如果你不这样做,就会发生不可预测的事情。

另外,如果您想创建一个不存在的文件,您将需要O_CREAT或,即

fd = open(tmpname, O_WRONLY | O_APPEND | O_CREAT, 0644);

O_CREAT(粗略地说)如果文件不存在则创建该文件。

从手册页:



NAME
       open, creat - open and possibly create a file or device

SYNOPSIS
       #include 
       #include 
       #include 

       int open(const char *pathname, int flags);
       int open(const char *pathname, int flags, mode_t mode);

       int creat(const char *pathname, mode_t mode);

...
       O_CREAT
              If the file does not exist it will be created.  The owner (user ID) of
              the  file  is  set to the effective user ID of the process.  The group
              ownership (group ID) is set either to the effective group  ID  of  the
              process  or  to  the  group  ID  of the parent directory (depending on
              filesystem type and mount options, and the mode of the  parent  direc‐
              tory,  see  the  mount  options  bsdgroups and sysvgroups described in
              mount(8)).

              mode specifies the permissions to use in case a new file  is  created.
              This  argument must be supplied when O_CREAT is specified in flags; if
              O_CREAT is not specified, then mode is ignored.  The effective permis‐
              sions  are  modified by the process's umask in the usual way: The per‐
              missions of the created file are (mode & ~umask).  Note that this mode
              applies  only to future accesses of the newly created file; the open()
              call that creates a read-only file may well return a  read/write  file
              descriptor.

  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 C 语言中使用 open() 创建文件 的相关文章

  • OpenSSL:RSA 加密/解密、密钥生成和密钥持久性

    我正在尝试构建一个需要以下内容的 p2p 应用程序 在 OpenSSL 中使用 RSA Encryption Decryption Generating Keys done Saving and loading keys done Savi
  • MVC 2视图显示错误的模型信息

    我在一个项目中使用 MVC 2 但我遇到了视图问题 在控制器中我有代码 return View calendarDay 如果我调试这一行并检查 calendarDay 它会告诉我 calendarDay Id 属性等于 2 在视图中我有一些
  • 嵌套绑定表达式

    这是一个后续问题我之前的问题 https stackoverflow com questions 2735294 templates function pointers and c0x include
  • 如何防止函数中的隐式转换?

    我正在编写一个实用程序类 其中包含 IsEquals 和 IsGreaterThanEquals 等接受 double 类型参数的方法 当我将浮点值发送到方法时 它们会隐式转换为双精度值并进行比较 我不希望这种事发生 当我发送 float
  • Motif 库的水平绘制的 RowColumn 类 (C)?

    我正在使用 Motif Library 来完成我的工作 如果有人不熟悉这个库 您可以在这里找到文件列表https packages ubuntu com xenial amd64 libmotif dev filelist https pa
  • 带有成员 (operator[]) 函数的 invoke_result

    如何为成员函数正确调用invoke result 或者专门用于运算符成员函数 我试过std invoke result
  • 改装和授权标头

    目前 我正在向我的请求添加授权标头 如下所示 文件 SomeFile cs public interface ITestApi Get api test id Task
  • 使用 MemoryCache 而不是普通的旧 Dictionary 的令人信服的理由是什么

    我刚刚遇到内存缓存 http msdn microsoft com en us library system runtime caching memorycache aspx这是 NET 4 中的新增功能 我知道如果你想的话它会很有用 限制
  • 在 Visual Studio 2017 mac 上安装扩展 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试在 Visual Studio for Mac 上安装 Visual Studio Marketplace 扩展 但是 Vi
  • “未定义对 clrscr() 的引用;” [复制]

    这个问题在这里已经有答案了 include
  • llvm clang 编译器上的dynamic_cast失败

    我看到一个奇怪的失败dynamic cast正在返回NULL在 clang 编译器上 但相同的代码可以在 gcc 环境下运行 您能否指出根本原因是什么 之间可能有什么区别dynamic cast关于 llvm 和 gcc 我正在使用两个编译
  • 在实体框架中不使用 Dispose 或 using()

    我一路上正在编写一个网络应用程序并学习实体框架 如果我做错了什么 我很好奇 我在查询时没有使用过 dispose 或 using 语句 我的存储库示例 public User GetUserById int sessionId var us
  • 从 Windows 选择声音并播放它们

    我有一个 WinForms 应用程序 该应用程序有一个 首选项 部分 用户可以在其中选择显示警报时播放哪些声音 是否可以有一个组合框 用户可以从 Windows 存储的声音中进行选择 例如 紧急停止 紧急蜂鸣 等 这些可以在 控制面板 gt
  • 静态成员函数中的封闭类的 C++ 类型

    我认为这是完全不可能的 但如果呢 在任何版本的 C 中 是否有可能以某种方式获取静态成员函数中封闭类的类型 class Impossible public static void Fun typedef Impossible Enclosi
  • 设置了 OFN_ALLOWMULTISELECT 标志的 GetOpenFileName()

    我正在尝试使用 GetOpenFileName 通用对话框调用来弹出打开对话框并允许用户选择多个文件 我设置了 OFN ALLOWMULTISELECT 标志 并设置了 OFN EXPLORER 因此我得到了 新样式 文件选择框 当我设置
  • 获取上下文菜单的控制

    我有一个如下所示的上下文菜单 A 1 2 3 选择 1 2 或 3 后 我需要访问调用上下文菜单的对象 意思是如果这是 textbox1 的上下文菜单 那么我需要访问该对象 我该怎么做 忘了说了 这是一个WPF应用程序 所以我使用 Syst
  • 如何在迭代时从地图中删除?

    迭代时如何从地图中删除 喜欢 std map
  • 对数据绑定组合框进行排序的最佳方法是什么?

    我对此做了一些研究 似乎对数据绑定组合框进行排序的唯一方法是对数据源本身进行排序 在本例中为数据集中的数据表 如果是这种情况 那么问题就变成对数据表进行排序的最佳方法是什么 组合框绑定在设计器中设置初始化使用 myCombo DataSou
  • 找出用户属于哪些组

    我有一个刚刚创建的 Windows 用户帐户 以 XYZ 为例 此 XYZ 属于我在计算机管理 gt 本地用户和组中创建的用户组和自定义组 因此 在属性中我看到该用户属于 2 个组 现在我想获取这些组并显示它们 有什么建议么 我已经这样做了
  • 如何用纯色填充位图?

    我需要使用唯一的 RGB 颜色创建 24 位位图 分辨率 100x100 像素 并将生成的图像保存到磁盘 我目前使用的是SetPixel http msdn microsoft com en us library 6c7eyzyb aspx

随机推荐