生活随笔
收集整理的這篇文章主要介紹了
系统I/O小程序-文件拷贝
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
系統(tǒng)I/O小程序-文件拷貝
使用系統(tǒng)IO函數(shù)編寫 ./syscopy src des
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc
, char *argv
[])
{int des_fd
, src_fd
;char buf
[128];int size
;unsigned int count
;if (argc
< 3) {printf("The argc is wrong!\n");return -1;}src_fd
= open(argv
[1], O_RDONLY
);if (src_fd
< 0) {printf("open %s fail.\n", argv
[1]);return -1;}des_fd
= open(argv
[2], O_RDWR
|O_CREAT
|O_TRUNC
);if (des_fd
< 0) {printf("open %s fail.\n", argv
[2]);close(src_fd
);return -1;}while (1) {size
= read(src_fd
, buf
, 128);if (size
== -1) {printf("read %s fail.\n", argv
[1]);close(src_fd
);close(des_fd
);return -1;}if (size
== 0) {if(errno
== EINTR
) {continue;}break;}if (write(des_fd
, buf
, size
) != size
) {printf("write %s fail.\n", argv
[2]);close(src_fd
);close(des_fd
);return -1;}}count
= lseek(src_fd
, 0, SEEK_END);printf("src long:%d\n", count
);count
= lseek(des_fd
, 0, SEEK_END);printf("des long:%d\n", count
);close(src_fd
);close(des_fd
);return 0;
}
總結(jié)
以上是生活随笔為你收集整理的系统I/O小程序-文件拷贝的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。