10.3 打开和关闭文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(char *filename, int flags, mode_t mode);
// 返回:若成功则为新文件描述符,若出错为 -1。fd = Open("foo.txt", O_RDONLY, 0);练习题 10.1
最后更新于
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(char *filename, int flags, mode_t mode);
// 返回:若成功则为新文件描述符,若出错为 -1。fd = Open("foo.txt", O_RDONLY, 0);最后更新于
fd = Open("foo.txt", O_WRONLY|O_APPEND, 0);#define DEF_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
#define DEF_UMASK S_IWGRP|S_IWOTHumask(DEF_UMASK);
fd = Open("foo.txt", O_CREAT|O_TRUNC|O_WRONLY, DEF_MODE);#include <unistd.h>
int close(int fd);
// 返回:若成功则为 0,若出错则为 -1。#include "csapp.h"
int main()
{
int fd1, fd2;
fd1 = Open("foo.txt", O_RDONLY, 0);
Close(fd1);
fd2 = Open("baz.txt", O_RDONLY, 0);
printf("fd2 = %d\n", fd2);
exit(0);
}