#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
void main(){
int fd,size;
char s[] = "hello world!" ,buffer[200];
/*打开 /home/yyf/Desktop/Linux 做写入,如果该文件不存在则建立该文件*/
/*参数O_WRONLY 以只写到方式打开文件*/
/*参数O_CREAT 若欲打开到文件不存在,则建立该文件*/
/*参数O_RDWR 以读写方式打开文件*/
/*参数O_RDONLY 以只读方式打开文件*/
fd = open("/home/yyf/Desktop/Linux.txt",O_RDWR|O_CREAT,00777);
if(fd == -1)
perror("fd");
write(fd,s,strlen(s));
close(fd);
/*打开 /home/yyf/Desktop/Linux 做读取动作*/
fd = open ("/home/yyf/Desktop/Linux.txt",O_RDONLY);
size = read(fd,buffer,sizeof(buffer));
if (size == -1)
perror("read");
close(fd);
printf("%s\n",buffer);
fd = open ("/home/yyf/Desktop/Linux.txt",O_RDONLY);
size = read(fd,buffer,sizeof(buffer));
if (size == -1)
perror("read");
close(fd);
printf("%s\n",buffer);