顾名思义,creat创建新文件,open打开文件。但,open也可以起到创建新文件的作用。
#include <fcntl.h> int creat(const char *pathname, mode_t mode); //Returns: file descriptor opened for write-only if OK, 1 on error
当creat的文件已存在时,creat将截断文件原文件,文件的修改时间访问时间也一并被修改。
creat相当于:open (pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
open
A file is opened or created by calling the open function.
#include <fcntl.h> int open(const char *pathname, int oflag, ... /*mode_t mode */ ); //Returns: file descriptor if OK, 1 on error
第三个参数只有在创建文件时才有用。
One deficiency with creat is that the file is opened only for writing. Before the new version of open was provided, if we were creating a temporary file that we wanted to write and then read back, we had to call creat, close, and then open. A better way is to use the open function, as in
open (pathname, O_RDWR | O_CREAT | O_TRUNC, mode);
from APUE
总的来说,open功能更强,包含了creat的功能。建议用open。
时间: 2024-12-11 15:22:20