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;
    }
  • 相关阅读:
    Python垃圾回收机制 总结
    求二叉树中任意两个结点的距离
    Python 实现排序算法
    InnoDB 索引原理
    Linux常用脚本命令总结
    How Django works?
    RabbitMQ 知识总结
    最短路径之Dijkstra算法和Floyd-Warshall算法
    最小生成树之Prim算法和Kruskal算法
    Python collections模块总结
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14875942.html
Copyright © 2011-2022 走看看