题目:
向文件f1中写入“hello world!”,然后将f1中的内容读出并显示在屏幕上
#include <stdio.h>
#include <sys/fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(){
int fd;
char buf1[13] ={"hello World!"};
int num;
char buf2[12];
fd = open("f1",O_RDWR|O_CREAT,0644);
if(fd == -1){
perror("error open f1 ...");
exit(1);
}
num = write(fd,buf1,sizeof(buf1)-1);
if(num != (sizeof(buf1)-1)){
printf("wirte less...");
}
lseek(fd,0,SEEK_SET);
num = read(fd,buf2,12);
if(num != 12){
printf("read less");
}
write(1,buf2,12);
close(fd);
}