【日精进】文件拷贝函数,你会写吗?

来源:互联网 发布:python源码剖析 第二版 编辑:程序博客网 时间:2024/05/17 07:18

方式:运用参数可变函数
定义两个文件指针:FILE*srcFile,FILE*destFile;
基本文件操作函数:fclose(),fopen(),fgetc(),fputc();
警告:打开一个文件时,要对结果进行判断是否打开

/*文件拷贝函数*/#include<stdio.h>int main(int argCount, char*argValue[]){    FILE*srcFile = NULL, *destFile = NULL;    int ch = 0;    if (argCount != 3)    {        printf("Usage:%s src-file-name dest-file-name\n", argValue[0]);    }    else    {    if ((srcFile = fopen(argValue[1], "r")) == 0)    {        printf("Can not open source file\"%s\"!", argValue[2]);    }    else if ((destFile = fopen(argValue[2], "W")) == 0)    {        printf("Can not open destination file\"%s\"!", argValue[2]);        fclose(srcFile);    }    else    {         while ((ch = fgetc(srcFile)) != EOF)fputc(ch, destFile);        printf("Successful to copy a file!\n");        fclose(srcFile);        fclose(destFile);        return 0;    }    }    return 1;}
0 0
原创粉丝点击