学习fread、fwrite函数

本文介绍了一个简单的C语言程序,用于从一个文件读取内容并将其复制到另一个文件中。该程序使用了标准输入输出库和字符串处理库,通过定义缓冲区大小进行逐块复制,实现了基本的文件复制功能。

一个拷贝的小程序

#include <stdio.h>
#include <string.h>

#define BUF_SIZE 1000
int main()
{
    FILE *fp_src,*fp_dst;
    size_t readed;
    char buf[BUF_SIZE];
    memset(buf,0,BUF_SIZE);
    if((fp_src=fopen("/root/test","rb"))==NULL)
    {
            fprintf(stderr,"open file1 failure!\r\n");
    }
    if((fp_dst=fopen("/root/test2","wb"))==NULL)
    {
            fprintf(stderr,"open file2 failure!\r\n");
    }
    while (!feof(fp_src)) {
        readed=fread(buf,sizeof(char),BUF_SIZE,fp_src);
        if(fwrite(buf,sizeof(char),BUF_SIZE,fp_dst)<readed)
        {
            //write file error! exit
            fprintf(stderr,"write file2 fialure!\r\n");
            return 0;
        }
    }
    fclose(fp_src);
    fclose(fp_dst);
    printf("copy test to test2 success! \r\n");
    return 0;
}

 

转载于:https://www.cnblogs.com/ZQQH/p/8401602.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值