zoukankan      html  css  js  c++  java
  • 54.文件按大小切割

    运行结果:

    完整代码:

      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include <stdlib.h>
      3 #include <stdio.h>
      4 
      5 #define sorPath "111.doc"
      6 char **stoPath;
      7 
      8 //获取文件大小
      9 int getSize(char *path)
     10 {
     11     FILE *pf = fopen(path, "r");
     12 
     13     if (pf)
     14     {
     15         fseek(pf, 0, SEEK_END);
     16         int length = ftell(pf);
     17         fclose(pf);
     18         return length;
     19     }
     20 
     21     return 0;
     22 }
     23 
     24 //切割文件
     25 void devide(char *path,int n)
     26 {
     27     //分配n个地址
     28     stoPath = (char **)malloc(sizeof(char *) * n);
     29     //每个地址分配内存,并且初始化
     30     for (int i = 0; i < n; i++)
     31     {
     32         stoPath[i] = (char *)malloc(1024);
     33         sprintf(stoPath[i], "%d.doc", i);
     34         printf("%s
    ", stoPath[i]);
     35     }
     36 
     37     //二进制打开源文件
     38     FILE *pfr = fopen(path, "rb");
     39     //获取大小
     40     int totalSize = getSize(path);
     41 
     42     //如果能被整除
     43     if (totalSize%n == 0)
     44     {
     45         for (int i = 0; i < n; i++)
     46         {
     47             FILE *pfw = fopen(stoPath[i], "wb");
     48             for (int j = 0; j < totalSize / n; j++)
     49             {
     50                 fputc(fgetc(pfr), pfw);
     51             }
     52             fclose(pfw);
     53         }
     54     }
     55     else
     56     {
     57         //如果不能被整除
     58         for (int i = 0; i < n - 1; i++)
     59         {
     60 
     61             FILE *pfw = fopen(stoPath[i], "wb");
     62             for (int j = 0; j < totalSize / n; j++)
     63             {
     64                 fputc(fgetc(pfr), pfw);
     65             }
     66             fclose(pfw);
     67         }
     68 
     69         FILE *pfw = fopen(stoPath[n-1], "wb");
     70         int left = totalSize - (totalSize / n) * (n - 1);
     71         for (int i = 0; i < left; i++)
     72         {
     73             fputc(fgetc(pfr), pfw);
     74         }
     75         fclose(pfw);
     76     }
     77     fclose(pfr);
     78 }
     79 
     80 //合并
     81 void merge(char *path, int n)
     82 {
     83     FILE *pfw = fopen(path, "wb");
     84 
     85     for (int i = 0; i < n; i++)
     86     {
     87         FILE *pf = fopen(stoPath[i], "rb");
     88         if (pf)
     89         {
     90             int ch;
     91             while ((ch = fgetc(pf)) != EOF)
     92             {
     93                 fputc(ch, pfw);
     94             }
     95         }
     96         fclose(pf);
     97     }
     98     fclose(pfw);
     99 }
    100 
    101 void main()
    102 {
    103     int length = getSize(sorPath);
    104 
    105     printf("%d
    ", length);
    106     devide(sorPath, 10);
    107     merge("last.doc", 10);
    108     system("pause");
    109 }
  • 相关阅读:
    POJ3268-Silver Cow Party-(Dijstra)
    POJ3259-Wormholes-( spfa || Bellman_Ford )
    POJ2139-Six Degrees of Cowvin Bacon-(Floyd_Warshall)
    hdu3974-Assign the task-(dfs+线段树)
    ZOJ3261-Connections in Galaxy War-(逆向并查集+离线处理)
    hdu1540-Tunnel Warfare-(线段树+二分)
    hdu4027-Can you answer these queries? -(线段树+剪枝)
    POJ3616-Milking Time-(dp)
    Visual Studio Code
    Visual Studio Code
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8426853.html
Copyright © 2011-2022 走看看