代码如下:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<sys/types.h> 4 #include<sys/stat.h> 5 #include<unistd.h> 6 #include<fcntl.h> 7 int main(){ 8 int fd=open("temp.txt",O_CREAT | O_RDWR,0777);//创建一个文件 9 if(fd==-1){ 10 perror("Open");//输出错误信息 11 exit(1);//退出程序 12 } 13 unlink("temp.txt");//删除临时的文件temp 14 write(fd,"我爱你哦!!!",50);//向当前的文件写入字符 15 lseek(fd,0,SEEK_SET);//重置指针的指向 16 char buff[1024];//定于一个缓存的数组 17 int len=read(fd,buff,1024);//读文件有多少个字节 18 write(1,buff,len);//将读出的内容写到屏幕上面 19 close(fd);//关闭临时的文件 20 }
程序执行完之后就被删除掉了