zoukankan      html  css  js  c++  java
  • c语言中实现文件的复制(文本复制和二进制复制)

    1、文本复制 fgetc、fputc函数实现

    #include <stdio.h>
    
    int main(void)
    {
        int ch;
        FILE *sfp;
        FILE *dfp;
        char sfilename[FILENAME_MAX];
        char dfilename[FILENAME_MAX];
        printf("source filename: "); scanf("%s", sfilename);
        printf("destination filename: "); scanf("%s", dfilename);
        
        if((sfp = fopen(sfilename, "r")) == NULL)
            printf("aSource file open failed.
    ");
        else
        {
            if((dfp = fopen(dfilename, "w")) == NULL)
                printf("aDestinatiom file open failed.
    ");
            else
            {
                while((ch = fgetc(sfp)) != EOF)
                    fputc(ch, dfp);
                fclose(dfp);
            }
            fclose(sfp);
        }
        return 0;
    }

    2、二进制复制、使用fread、fwrite函数实现

    #include <stdio.h>
    
    int main(void)
    {
        int ch;
        FILE *sfp;
        FILE *dfp;
        char sfilename[FILENAME_MAX];
        char dfilename[FILENAME_MAX];
        printf("Source filename: "); scanf("%s", sfilename);
        printf("Destination filename: "); scanf("%s", dfilename);
        
        if((sfp = fopen(sfilename, "rb")) == NULL)
            printf("aSource file open failed.
    ");
        else
        {
            if((dfp = fopen(dfilename, "wb")) == NULL)
                printf("aDestination file open failed.
    ");
            else
            {
                int n;
                while(n = (fread(&ch, sizeof(int), 1, sfp)) > 0)
                    fwrite(&ch, sizeof(int), 1, dfp);
                fclose(dfp);    
            } 
            fclose(sfp);
        }
        return 0;
    }
  • 相关阅读:
    面试常考知识点——Java(JVM,JDK,JRE)
    使用CSS transform属性的skewX、skewY沿X、Y轴倾斜元素
    视觉设计应用
    小程序
    小程序获取用户的登录头像和用户名
    整站建设
    2018-05-05(在小程序中使用图标)
    2018-05-03(PHP)
    webpack
    简历
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14875942.html
Copyright © 2011-2022 走看看