linux应用编程学习(2)系统调用文件类

二、 系统调用文件类

文件描述符: 在中国,每一个成年的公民都会有一个身份证编号,它的本质就 是一个数字,我们可以利用这个数字来标记这个公民。在Linux系 统中,所有打开的文件也对应一个数字,这个数字由系统来分配, 我们称之为:文件描述符。


2.1.打开文件:          int open(const char *pathname,int flags);

                             int open(const char *pathname,int flags,mode_t mode)

                             打开或者创建一个文件

    所属头文件:         sys/types.h    sys/stat.h   fnctl.h

    返回值:              成功:文件描述符    失败:-1

    参数说明:           pathname: 要打开的文件名(含路径)

                           flags: 文件打开标志

                           -O_APPEND:以追加方式打开文件

                           -O_CREAT:当打开的文件不存在的时候,创建该文件

                          mode:一定是在flags中使用了O_CREAT标志,mode记录待创建文件的访问权限.

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>

  4. void main()
  5. {
  6.     int fd;
  7.     char *pathname = "./test.c";
  8.     fd = open(pathname, O_RDWR|O_CREAT, 00666);

  9.     if(fd <0)
  10.         printf("open file fail!\n");

  11. }


2.2.创建文件:         int creat (const char *pathname,mode_t mode); 

                           创建一个文件,并以只写的方式打开该文件

     所属头文件:       sys/types.h sys/stat.h fcntl.h

     返回值:           成功:只写打开的文件描述符    失败:-1

     参数说明:        Pathname:要创建的文件名(含路径)

                           Mode:创建文件的读写权限.

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>

  4. void main()
  5. {
  6.     int fd;
  7.     char *pathname = "./test.c";

  8.     fd = creat(pathname, 0666);

  9.     if(fd<0)
  10.     {
  11.         printf("file creat failed!\n");
  12.     }

  13. }


2.3.关闭文件:       Int close(int filedes);

                          关闭一个打开的文件,释放进程在文件上的记录锁

     所属头文件:    unistd.h

     返回值:          成功:0 出错-1

     参数说明:      Filedes:待关闭的文件描述符

  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>


  5. void main()
  6. {
  7.     int fd;
  8.     int ret;
  9.     char *pathname = "./test.c";

  10.     fd = open(pathname, O_RDWR|O_CREAT, 0666);

  11.     if(fd<0)
  12.         printf("file open fialed!\n");
  13.     
  14.     ret = close(fd);
  15.     if(ret<0)
  16.         printf("file close failed!\n");
  17. }


2.4.读文件:         ssize_t read(int fd, void *buf, size_t count);

                         从打开文件中读数据

     所属头文件:   unistd.h

     返回值:         成功:返回读到的字节数.若已到文件结尾返回0,失败-1

     参数说明:      fd:要读取数据的文件描述符

                        count:要读取的字节数

                        buf:指向来的数据存到buf指向的空间

  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>

  5. void main()
  6. {
  7.     int fd;
  8.     char *pathname = "./test.c";
  9.     size_t count = 10;
  10.     size_t read_ret;
  11.     char buf[20];

  12.     fd = open(pathname, O_RDWR|O_CREAT, 0666);
  13.     if(fd<0)
  14.         printf("file open failed!\n");

  15.     read_ret = read(fd,buf,count);
  16.     if(read_ret <0)
  17.         printf("file read failed!\n");
  18.     else
  19.     {
  20.         buf[count] = '\0';
  21.         printf("the buf is %s\n",buf);
  22.     }

  23.     close(fd);
  24. }


2.5.写文件:          ssize_t write(int fd, const void *buf, size_t count);

                          向打开的文件写数据

     所属头文件:    unistd.h

     返回值:          成功:返回已写的字节数,出错:-1

     参数说明:       fd:要写入数据的文件的描述符

                         buf:要写入的数据的存放位置

                         count:希望写入的字节数


  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>


  5. void main()
  6. {
  7.     int fd;
  8.     char *buf = "this is my string.";
  9.     size_t count = 18;
  10.     ssize_t write_ret;

  11.     fd = open("./test.c",O_RDWR|O_CREAT, 0666);
  12.     if(fd < 0)
  13.         printf("file open failed\n");

  14.     write_ret = write(fd, buf, count);
  15.     if(write_ret < 0)
  16.         printf("file write failed\n");

  17.     close(fd);
  18. }


 

2.6.定位文件:      off_t lseek(int fd, off_t offset, int whence);

                         重新定位文件读写位置

     所属头文件:    sys/types.h unistd.h

     返回值:          成功:返回移动后的文件指针,距离文件头的位置.   失败:-1

     参数说明:       fd:文件描述符

                         offset:便宜数

                         Whence:起始位置   

                            SEEK_SET:从文件开始处offset个字节 

                            SEEK_CUR:从当前偏移量加offset  

                            SEEK_END:从文件长度加offset,可以为正负

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>

  5. void main()
  6. {
  7.     int fd;
  8.     char *pathname = "./test.c";
  9.     char *w_buf = "this is my string.";
  10.     char r_buf[20];
  11.     size_t count = 18;
  12.     ssize_t w_ret;
  13.     ssize_t r_ret;
  14.     off_t ls_ret;

  15.     fd = open(pathname, O_RDWR|O_CREAT, 0666);
  16.     if(fd < 0)
  17.         printf("file open fialed!\n");

  18.     w_ret = write(fd, w_buf, count);
  19.     if(w_ret < 0)
  20.         printf("file write fialed!\n");

  21.     ls_ret = lseek(fd, 0, SEEK_SET);
  22.     if(ls_ret < 0)
  23.         printf("file lseek fialed!\n");

  24.     r_ret = read(fd, r_buf, 10);
  25.     if(r_ret < 0)
  26.         printf("file read fialed!\n");
  27.     else
  28.     {
  29.         r_buf[10] = '\0';
  30.         printf("file read is %s\n", r_buf);
  31.     }

  32.     close(fd);
  33. }



2.7.复制文件描述符:        int dup(int oldfd);

                                    int dup2(int oldfd, int newfd);

                                    复制一个文件描述符

     所属头文件:              unistd.h

     返回值:                    成功:返回新的文件描述符       失败:-1

     参数说明:                 oldfd:需要复制的文件描述符
                                    Newfd:新文件描述符

  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>

  5. void main()
  6. {
  7.     int fd;
  8.     int fd1;
  9.     char *pathname = "./test.c";
  10.     char *w_buf = "this is my string.";
  11.     char r_buf[20];
  12.     size_t count = 18;
  13.     ssize_t w_ret;
  14.     ssize_t r_ret;
  15.     off_t ls_ret;

  16.     fd = open(pathname, O_RDWR|O_CREAT, 0666);
  17.     if(fd < 0)
  18.          printf("file open failed!\n");

  19.     fd1 = dup(fd);

  20.     w_ret = write(fd1, w_buf, count);
  21.     if(w_ret < 0)
  22.         printf("file write fialed!\n");

  23.     ls_ret = lseek(fd1, 0, SEEK_SET);
  24.     if(ls_ret < 0)
  25.         printf("file lseek fialed!\n");

  26.     r_ret = read(fd1, r_buf, 10);
  27.     if(r_ret < 0)
  28.         printf("file read fialed!\n");
  29.     else
  30.     {
  31.         r_buf[10] = '\0';
  32.         printf("file read is %s\n", r_buf);
  33.     }

  34.     close(fd1);
  35. }
编写一个复制文件功能的函数:

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>

  5. void main(int argc, char *argv[])
  6. {
  7.     int fd_s;
  8.     int fd_t;
  9.     int count = 0;
  10.     char buf[512];

  11.     if(argc != 3)
  12.     {
  13.         printf("usage: filecp [srcfile] [detfile]\n");
  14.         return;
  15.     }

  16. /*1.打开源文件*/
  17.     fd_s = open(argv[1], O_RDONLY);

  18. /*2.打开目标文件*/
  19.     fd_t = open(argv[2], O_RDWR|O_CREAT, 0666);

  20. /*3.读取源文件数据*/
  21.     while((count = read(fd_s, buf, 512)) > 0)
  22.     {
  23.         write(fd_t, buf, count); /*4.将数据写入目标文件*/
  24.     }

  25. /*5.关闭文件*/
  26.     close(fd_s);
  27.     close(fd_t);
  28. }


<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(210) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

习惯就好zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值