liunx系统编程中的open(),close(),lseek(),write(),read()及相关例子

Linux系统调用级别的函数有许多,这里只说明open(),close(),lseek(),write()和read()函数。

open()原型

int open(const char *pathname,flags,intperms)

参数定义:

pathname:路径名,例子: “/tmp/working/open.c”

             flags:文件打开方式。

                            O_RDONLY: 只读方式

                            O_WRONLY:只写方式

                            O_RDWR:读写方式  这三个是不能同时存在

 

                            O_CREAT:如果路径下没有该文件,如没有open.c,则创建open.c文件。

                            O_EXCL:如果使用了O_CREAT这个参数且文件存在时,返回错误。

                            O_NOCTTY:

                            O_TRUNC:如果文件存在,并且以只读或者只写成功打开,则会删除文件的数据。

                            O_APPEND:以添加方式打开文件,在打开文件的同时,文件指针指向文件的末尾。

 

                   perms:打开的存取权限。

返回值:成功:文件描述符

       失败:-1

 

close()原型:

int close(int fd)

fd是文件描述符,是open()函数的返回值。

返回值:成功:0

                   失败:-1

 

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

#include<stdio.h>

 

int main()

{

         intfd;

         if((fd= open("./hello.txt" , O_CREAT | O_TRUNC | O_WRONLY , 0666)) < 0)

         {

                   perror("open:");

                   exit(1);

         }

         else

         {

                   printf("Openfile : hello.c %d\n",fd);

         }

         if(close(fd) < 0 )

         {

                   perror("close:");

                   exit(1);

         }

         else

         {

                   printf("Closehello.c\n");

         }

         exit(0);

}


上面的程序起打开文件作用,如果没有,就创建。

read()函数

原型:ssize_t read(int fd,void *buf,size_t count)

fd:文件描述符

buf:指定存储器读出数据的缓存区

count:指定读出的字节数

返回值:

         成功:读到的字节数。

         0:达到了文件尾

         -1:出错

 

write()函数

原型:ssize_t write(int fd,void *buf,size_t count)

         fd:文件描述符

         buf:指定存储器写入数据的缓冲区

         count:指定读出的字节数

返回值:

         成功:已经写入的字数

         -1:出错

 

lseek:

原型:off_t lseek(int fd,off_t offset,int whence)

         fd:文件描述符

         offset:偏移量,单位是字节,可正可负。

whence:

                   SEEK_SET:文件头

                   SEEK_CUR:但钱位置

                   SEEK_END:文件尾

返回值:成功:文件当前位置

                   -1:出错

write.c文件

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

 

int main(void)

{

         inti,fd,size,len;

         char*buf="Hello world,I will write this file!";

         if((fd= open("./hello.txt",O_CREAT | O_TRUNC | O_RDWR,0777 ))<0)

         {

                   perror("open:");

                   exit(1);

         }       

         else

                   printf("openhello.txt %d\n:",fd);

         len= strlen(buf);

         if((size=write( fd , buf , len )) < 0 )

         {

                   perror("write:");

                   exit(1);

         }

         else

                   printf("Write%s \n",buf);

         if(close(fd) < 0 )

         {

                   perror("close:");

                   exit(1);

         }

         else

                   printf("Closehello.txt\n");

         exit(0);

}


上面的程序就是创建一个文件,写入buf里面的内容。

 

 

read.c文件

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

int main(void)

{

         intfd,len,size;

         charbuf_r[100];

         //if((fd=open("./hello.c",O_CREAT| O_RDWR,0777))<0)

         if((fd= open("./hello.txt",O_CREAT | O_RDWR,0777 ))<0)

         {

                   perror("open");

                   exit(1);

         }

         else

                   printf("openthe file hello.txt \nfd is %d \n",fd);

         len= lseek(fd,0,SEEK_END);

         printf("lenis %d\n",len);

         lseek(fd,0,SEEK_SET);     

         if((size= read(fd,buf_r,len))<0)

         {

                   perror("read");

                   exit(1);

         }

         else

         {

                   buf_r[len]='\0';                  

                   printf("sizeis %d\n",size);

                   printf("readfile is %s\n",buf_r);

                  

         }

         if(close(fd)<0)

         {

                   perror("close");

                   exit(1);

         }

         else

                   printf("thefile is close\n");

         exit(0);

        

}


其中len = lseek(fd,0,SEEK_END);

这段是测试文件长度的程序。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值