#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 = "longhaiyang hengshuai!";
fd = open("./file1",O_RDWR);
if(fd == -1){
printf("文件打开失败!\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd >0){
printf("创建文件成功!\n");
}
}
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file1!\n",n_write);
}
char *readbuf;
readbuf = (char *)malloc(sizeof(char)*n_write+1);
lseek(fd,0,SEEK_SET);
int n_read = read(fd,readbuf,n_write);
printf("read %d byte to readbuf,context:%s\n",n_read,readbuf);
close(fd);
}
```