檔案IO操作編程

來源:互聯網
上載者:User

通過一個簡單的copy程式,完成檔案的複製程式,瞭解基本的檔案I/O檔案讀寫的基本步驟
./command file1 file2 把檔案1的內容複寫到檔案2如果檔案2不存在則建立
------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define size 256
int main(int argc,char **argv)
{
 if(argc!=3)
 {
  fprintf(stderr,"usage %s file1 file2/n",argv[0]);
  exit(1);
 }
 char buf[size];
 int byte=0;
 int fd1,fd2;
 fd1=open(argv[1],O_RDONLY,0777);
 if(fd1<0)
 {
  perror("open");
  exit(1);
 }
 fd2=open(argv[2],O_WRONLY|O_CREAT,0777);//if file2 not exit creat it
 if(fd2<0)
 {
  perror("open");
  exit(1);
 }
 while(1)
 {
  byte=read(fd1,buf,size);
  write(fd2,buf,byte);
  if(byte==0)//if byte=0,read file tail
  break;
 }
 close(fd2);
 close(fd1);
}
----------------------------------------------------------------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.