Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7540627
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-06-09 21:05:25

  1. /*
  2.  *    低级文件编程
  3.  *    打开创建一个文件并设定其权限
  4.  *    Lzy        2011-6-9
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <fcntl.h>
  9. #include <sys/stat.h>

  10. int main(void)
  11. {
  12.     int fd;                        //定义文件描述符
  13.     
  14.     /*创建文件设置权限为777,但最后并不是777,这是因为umask*/
  15.     fd = open("lzy.txt", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IROTH);        //创建lzy文件,可读写打开文件
  16.     if(fd < 0)                    //打开是否成功
  17.     {
  18.         perror("open error");    //在标准输出打印出错误信息
  19.         exit(-1);
  20.     }

  21.     close(fd);                    //关闭文件
  22.     return 0;
  23. }
  1. /*
  2.  *    低级文件编程
  3.  *    写入字符串至文件
  4.  *    Lzy        2011-6-9
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <fcntl.h>
  10. #include <sys/stat.h>
  11. #include <error.h>

  12. int main(void)
  13. {
  14.     int fd_1;                        //定义文件描述符
  15.     char buf[100];                        //临时缓冲区
  16.     
  17.     fd_1 = open("lzy_1.txt", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IROTH);    //可读写打开文件
  18.     if(fd_1 < 0)                    //打开是否成功
  19.     {
  20.         perror("open fd_1 error");    //在标准输出打印出错误信息
  21.         exit(-1);
  22.     }
  23.     
  24.     
  25.     printf("输入字符:");
  26.     scanf("%s",buf);

  27.     if(write(fd_1, buf, strlen(buf)) == -1)        //把字符串写入文件
  28.     {
  29.         perror("write error");
  30.         exit(-1);
  31.     }
  32.     
  33.     close(fd_1);                    //关闭文件
  34.     return 0;
  35. }
  1. /*
  2.  *    低级文件编程
  3.  *    文件复制
  4.  *    Lzy        2011-6-9
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <fcntl.h>
  10. #include <sys/stat.h>
  11. #include <error.h>

  12. int main(int argc, char *argv[])
  13. {
  14.     int fd_1, fd_2;                        //定义文件描述符
  15.     char buf[100];                        //临时缓冲区
  16.     int ctr, ctw;

  17.     if(argc != 2)                        //检查命令行参数个数
  18.     {
  19.         printf("参数太少\n");
  20.         exit(0);
  21.     }
  22.     
  23.     fd_1 = open("lzy_1.txt", O_RDONLY);    //可读方式打开源文件
  24.     if(fd_1 < 0)                        //打开是否成功
  25.     {
  26.         perror("open fd_1 error");        //在标准输出打印出错误信息
  27.         exit(-1);
  28.     }

  29.     fd_2 = open(argv[1],O_WRONLY | O_CREAT, 00766);    //打开目标文件,不存在创建
  30.     if(fd_2 < 0)
  31.     {
  32.         perror("open fd_2 error");        //在标准输出打印出错误信息
  33.         exit(0);
  34.     }
  35.     
  36.     
  37.     /*文件拷贝*/
  38.     while((ctr = read(fd_1, buf, sizeof(buf))) != 0)        //读文件直至文件结束
  39.     {
  40.         if(ctr == -1)                //读发生错误
  41.         {
  42.             perror("read fd_1 error");        
  43.             exit(0);
  44.         }
  45.         else
  46.         {
  47.             ctw = write(fd_2, buf, ctr);    //写入文件
  48.             if(ctw == -1)
  49.             {
  50.                 perror("write fd_2 error");        
  51.                 exit(0);
  52.             }            
  53.         }
  54.     }
  55.     
  56.     close(fd_2);
  57.     close(fd_1);                    //关闭文件
  58.     return 0;
  59. }
阅读(2266) | 评论(0) | 转发(3) |
0

上一篇:低级IO映射到高级IO

下一篇:目录操作

给主人留下些什么吧!~~