1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("./file1",O_RDONLY|O_CREAT|O_EXCL,0600);
if(fd==-1){
printf("creat exit!\n");
}
return 0;
}
2
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf=“cao kuangyuan hen shuai!”;
fd = open("./file1",O_RDWR|O_APPEND);
printf("open success : fd = %d\n",fd);
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd,buf,strlen(buf));
if(n_write!=-1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
return 0;
}
3
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf=“TEXT”;
fd = open("./file1",O_RDWR|O_TRUNC);
printf("open success : fd = %d\n",fd);
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd,buf,strlen(buf));
if(n_write!=-1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
return 0;
}
4
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf=“TEXT”;
fd = creat("./file2",S_IRWXU);
//int creat(const char *pathname, mode_t mode);
close(fd);
return 0;
}