这是我第一次使用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(使用前将#替换为@)