Chinaunix首页 | 论坛 | 博客
  • 博客访问: 241290
  • 博文数量: 35
  • 博客积分: 791
  • 博客等级: 军士长
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-05 16:56
文章分类
文章存档

2013年(7)

2012年(28)

我的朋友

分类: 嵌入式

2012-10-05 21:18:56

程序要求:实现文件内容拷贝。
程序如下:

点击(此处)折叠或打开

  1. #include <sys/stat.h>
  2.     #include <fcntl.h>
  3.     #include <stdio.h>
  4.     #include <errno.h>
  5.       
  6.     #define BUFFER_SIZE 1024
  7.       
  8.     int main(int argc, char *argv[])
  9.     {
  10.       int from_fd; /* 源文件描述符 */
  11.       int to_fd; /* 目的文件描述符 */
  12.       int bytes_read; /* 读到的字节数 */
  13.       int bytes_write; /* 写入的字节数 */
  14.       char buffer[BUFFER_SIZE]; /* 写文件时的缓冲区大小 */
  15.       char *ptr;
  16.       
  17.       if( argc != 3 )
  18.         {
  19.                 fprintf( stderr,"Usage:%s fromfile tofile\n",argv[0] );
  20.                 exit(1);
  21.         }
  22.       
  23.       /* 打开源文件*/
  24.       if( (from_fd = open(argv[1],O_RDONLY)) == -1 )
  25.         {
  26.                 fprintf( stderr,"Open %s Error:%s\n",argv[1],strerror(errno) );
  27.                 exit(1);
  28.         }
  29.       
  30.       /* 打开目的文件,不存在的话就首先创建 */
  31.       if( (to_fd = open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR)) == -1 )
  32.         {
  33.                 fprintf( stderr,"Open %s Error:%s\n",argv[2],strerror(errno) );
  34.                 exit(1);
  35.         }
  36.       
  37.       /* 经典的文件拷贝 */
  38.       while( bytes_read = read( from_fd,buffer,BUFFER_SIZE))
  39.         {
  40.                 if( (bytes_read == -1)&&(errno != EINTR)) /* 一个致命的错误发生了 */
  41.                     break;
  42.                 else
  43.                 if( bytes_read > 0 )
  44.                         {
  45.                         ptr = buffer;
  46.                          while( bytes_write = write( to_fd,ptr,bytes_read ) )
  47.                                 {
  48.                                 if( (bytes_write == -1)&&(errno != EINTR) ) /* 一个致命的错误发生了 */
  49.                                                     break;
  50.                                 else
  51.                                     if( bytes_write == bytes_read ) /* 写完了所有独读到的字节 */
  52.                                                  break;
  53.                                             else
  54.                                             if(bytes_write > 0) /* 只写了一部分 */
  55.                                                      {
  56.                                                        ptr += bytes_write;
  57.                                                        bytes_read -= bytes_write;
  58.                                                  }
  59.                                  }
  60.                 if(bytes_write == -1)
  61.                          break;
  62.       
  63.                   }
  64.          }
  65.         
  66.       close(from_fd);
  67.       close(to_fd);
  68.       exit(0);
  69.     }


阅读(2466) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~