在linux下实现文件复制的功能

来源:互联网 发布:java中介者模式 编辑:程序博客网 时间:2024/06/14 07:48
#include <stdio.h>
#include <string.h>


int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        printf("error");
    }
    FILE* f1 = fopen(argv[1], "r+");
    if(NULL == f1)
    {
        perror("fopen");
        return 1;
    }


    FILE* f2 = fopen(argv[2], "w+");
    if(NULL == f2)
    {
        perror("fopen");
        return 2;
    }
    char str1[1024] = {0};


    int count = 0;
    while(count = fread(str1, sizeof(char), 1024, f1))
    {
        if(-1 == count)
        {
            perror("fread");
            fclose(f1);
            fclose(f2);
            return 4;
        }
        int count1 = fwrite(str1, sizeof(char), count, f2);
        if(-1 == count1)
        {
            perror("fwrite");
            fclose(f1);
            fclose(f2);
            return 5;
        }
        memset(str1, 0, 1024);
    }


    fclose(f1);
    fclose(f2);
    return 0;
}
阅读全文
1 0