cpp.c

来自「在linux下使用c语言编写的一个文件拷贝的源码」· C语言 代码 · 共 81 行

C
81
字号
#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <string.h>#define BUFFER_SIZE 1024int main(int argc, char **argv){    int SourceFile, DestFile;    int BytesReaded, BytesWrited;    int status;    char desc;    char Buffer[BUFFER_SIZE];    char *ptr;    /*  Not Enough Parameters */    if (argc != 3) {	printf("Usage Error!:%s fromfile tofile\n\a", argv[0]);	exit(1);    }    /*  Source File Not Exit */    if (status = access(argv[1], F_OK)) {	printf("The Source File Not Exit.\n");	exit(1);    }    /*  Destination File Exit */    if ((status = access(argv[2], F_OK)) == 0) {	printf("The Destination File Exit.Overwrite?(y/n)\n");	desc = getchar();	while (desc != 'n' && desc != 'y') {	    printf("The Destination File Exit.Overwrite?(y/n)\n");	    desc = getchar();	}	if (desc == 'n')	    exit(0);    }    /*  Open the Source File  */    if ((SourceFile = open(argv[1], O_RDONLY)) == -1) {	printf("Open %s Error:%s\n", argv[1], strerror(errno));	exit(1);    }    /*  Open the Destination File   */    if ((DestFile =	 open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {	printf("Open %s Error:%s\n", argv[2], strerror(errno));	exit(1);    }    /*  Read content from the Fromfile  */    while (BytesReaded = read(SourceFile, Buffer, BUFFER_SIZE)) {	/*  An errot occurt when reading    */	if ((BytesReaded == -1) && (errno != EINTR))	    break;	else if (BytesReaded > 0) {	    ptr = Buffer;	    while (BytesWrited = write(DestFile, ptr, BytesReaded)) {		/*  An errot occurt when writing  */		if ((BytesWrited == -1) && (errno != EINTR))		    break;		/*  Finish  */		else if (BytesWrited == BytesReaded)		    break;	    }	    /*  An errot occurt when writing  */	    if (BytesWrited == -1)		break;	}    }    close(SourceFile);    close(DestFile);    exit(0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?