fopen: r r+ w w+ a a+要求写出open函数对应打开方式的组合;
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
//r
printf("r--->");
int fd_r=open("r.c", O_RDONLY, 0664);
if(fd_r<0){
perror("open");
return -1;
}
printf("fd_r=%d\n",fd_r);
//r+
printf("rw--->");
int fd_rw=open("rw.c", O_RDWR, 0664);
if(fd_rw<0){
perror("open");
return -1;
}
printf("fd_rw=%d\n",fd_rw);
//w
printf("w--->");
int fd_w=open("w.c", O_WRONLY | O_CREAT | O_TRUNC, 0664);
if(fd_w<0){
perror("open");
return -1;
}
printf("fd_w=%d\n",fd_w);
//w+
printf("w+--->");
int fd_wr=open("wr.c", O_RDWR | O_CREAT | O_TRUNC, 0664);
if(fd_wr<0){
perror("open");
return -1;
}
printf("fd_wr=%d\n",fd_wr);
//a
printf("a--->");
int fd_aw=open("aw.c", O_WRONLY | O_APPEND | O_CREAT, 0664);
if(fd_aw<0){
perror("open");
return -1;
}
printf("fd_aw=%d\n",fd_aw);
//a+
printf("awr--->");
int fd_awr=open("awr.c", O_RDWR | O_APPEND | O_CREAT, 0664);
if(fd_awr<0){
perror("open");
return -1;
}
printf("fd_awr=%d\n",fd_awr);
return 0;
}