#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#define BUFFER_SIZE 1024
int main(void)
{
int fd,write_size;
char buffer[BUFFER_SIZE]="hello world";
if((fd = open("text.txt",O_CREAT|O_RDWR,S_IRWXU))==-1)
{
fprintf(stderr,"open error %d",strerror(errno));
}
lseek(fd,0,0);//第一个0表示偏移量,第二个零表示开头,1表示当前,2表示结尾。返回值为当前位置
if((write(fd,buffer,BUFFER_SIZE))==-1)
printf("write error ");
if((read(fd,buffer,BUFFER_SIZE))==-1)
printf("write error ");
printf("%s\n",buffer);
close(fd);
return 0;
}
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#define BUFFER_SIZE 1024
int main(void)
{
int fd,write_size;
char buffer[BUFFER_SIZE]="hello world";
if((fd = open("text.txt",O_CREAT|O_RDWR,S_IRWXU))==-1)
{
fprintf(stderr,"open error %d",strerror(errno));
}
lseek(fd,0,0);//第一个0表示偏移量,第二个零表示开头,1表示当前,2表示结尾。返回值为当前位置
if((write(fd,buffer,BUFFER_SIZE))==-1)
printf("write error ");
if((read(fd,buffer,BUFFER_SIZE))==-1)
printf("write error ");
printf("%s\n",buffer);
close(fd);
return 0;
}