• 63.当当网txt数据按行切割与合并


    • 获取文件有多少行
       1 //获取文件有多少行
       2 int getN(char *path)
       3 {
       4     FILE *pf = fopen(path, "r");
       5     if (pf==NULL)
       6     {
       7         return -1;
       8     } 
       9     else
      10     {
      11         int i = 0;
      12         while (!feof(pf))
      13         {
      14             char str[256] = { 0 };
      15             fgets(str, 256, pf);
      16             i++;
      17         }
      18         fclose(pf);
      19         return i;
      20     }
      21 }
    • 宏定义行数
      1 //数据的行数
      2 #define N 13180820
    • 文件按行切割
       1 //文件切割
       2 void space(char *path, int num)
       3 {
       4     char ** pathes = malloc(sizeof(char*)*num);
       5     for (int i = 0; i < num;i++)
       6     {
       7         pathes[i] = malloc(sizeof(char) * 256);
       8         //格式化处理文件名
       9         sprintf(pathes[i], "dangdangwang%d.txt", i + 1);
      10     }
      11     
      12     //打开文件
      13     FILE *pf = fopen(path, "r");
      14 
      15     if (pf == NULL)
      16     {
      17         return -1;
      18     }
      19     else
      20     {
      21         //如果能被整除
      22         if (N%num == 0)
      23         {
      24             for (int i = 0; i <  num;i++)
      25             {
      26                 //写入文件
      27                 FILE *pfw = fopen(pathes[i], "w");
      28                 for (int j = 0; j < N / num; j++)
      29                 {
      30                     char str[1024] = { 0 };
      31                     //读取一行写入一行
      32                     fgets(str, 1024, pf);
      33                     fputs(str, pfw);
      34                 }
      35                 fclose(pfw);
      36             }
      37         }
      38         else
      39         {
      40             for (int i = 0; i < num - 1; i++)
      41             {
      42                 //写入文件
      43                 FILE *pfw = fopen(pathes[i], "w");
      44                 //处理前面n-1个
      45                 for (int j = 0; j < N / (num-1); j++)
      46                 {
      47                     char str[1024] = { 0 };
      48                     //读取一行写入一行
      49                     fgets(str, 1024, pf);
      50                     fputs(str, pfw);
      51                 }
      52                 fclose(pfw);
      53 
      54             }
      55 
      56             {
      57                 //处理剩下的
      58                 //写入
      59                 FILE *pfw = fopen(pathes[num-1], "w");
      60                 for (int j = 0; j < N %(num-1); j++)
      61                 {
      62                     char str[1024] = { 0 };
      63                     //读取一行写入一行
      64                     fgets(str, 1024, pf);
      65                     fputs(str, pfw);
      66                 }
      67                 fclose(pfw);
      68             }
      69         }
      70         fclose(pf);
      71     }
      72 }
    • 文件合并
       1 //合并文件
       2 void merge(char *newpath,int n)
       3 {
       4     char ** pathes = malloc(sizeof(char*)*n);
       5     for (int i = 0; i < n; i++)
       6     {
       7         pathes[i] = malloc(sizeof(char) * 256);
       8         sprintf(pathes[i], "dangdangwang%d.txt", i + 1);
       9     }
      10 
      11 
      12     //写入文件
      13     FILE *pf = fopen(newpath, "w");
      14     if (pf == NULL)
      15     {
      16         return -1;
      17     }
      18     else
      19     {
      20         //依次读取每个文件
      21         for (int i = 0; i < n;i++)
      22         {
      23             FILE *pfr = fopen(pathes[i], "r");
      24 
      25             while (!feof(pfr))
      26             {
      27                 char str[1024] = { 0 };
      28                 //读取一行写入一行
      29                 fgets(str, 1024, pfr);
      30                 fputs(str, pf);
      31             }
      32             fclose(pfr);
      33         }
      34         fclose(pf);
      35     }
      36 }
    • 测试函数
       1 void main()
       2 {
       3     //int num = getN(path);
       4     //printf("%d", num);获取行数
       5     int  num;
       6     scanf("%d", &num);
       7 
       8     //分割
       9     space(path, num);
      10     //合并
      11     merge(newpath, num);
      12 
      13     system("pause");
      14 }

    完整代码:

      1 #define  _CRT_SECURE_NO_WARNINGS
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 char *path = "dangdangwang.txt";
      5 char *newpath = "dangdangwangN.txt";
      6 
      7 //数据的行数
      8 #define N 13180820
      9 
     10 //获取文件有多少行
     11 int getN(char *path)
     12 {
     13     FILE *pf = fopen(path, "r");
     14     if (pf==NULL)
     15     {
     16         return -1;
     17     } 
     18     else
     19     {
     20         int i = 0;
     21         while (!feof(pf))
     22         {
     23             char str[256] = { 0 };
     24             fgets(str, 256, pf);
     25             i++;
     26         }
     27         fclose(pf);
     28         return i;
     29     }
     30 }
     31 
     32 //文件切割
     33 void space(char *path, int num)
     34 {
     35     char ** pathes = malloc(sizeof(char*)*num);
     36     for (int i = 0; i < num;i++)
     37     {
     38         pathes[i] = malloc(sizeof(char) * 256);
     39         //格式化处理文件名
     40         sprintf(pathes[i], "dangdangwang%d.txt", i + 1);
     41     }
     42     
     43     //打开文件
     44     FILE *pf = fopen(path, "r");
     45 
     46     if (pf == NULL)
     47     {
     48         return -1;
     49     }
     50     else
     51     {
     52         //如果能被整除
     53         if (N%num == 0)
     54         {
     55             for (int i = 0; i <  num;i++)
     56             {
     57                 //写入文件
     58                 FILE *pfw = fopen(pathes[i], "w");
     59                 for (int j = 0; j < N / num; j++)
     60                 {
     61                     char str[1024] = { 0 };
     62                     //读取一行写入一行
     63                     fgets(str, 1024, pf);
     64                     fputs(str, pfw);
     65                 }
     66                 fclose(pfw);
     67             }
     68         }
     69         else
     70         {
     71             for (int i = 0; i < num - 1; i++)
     72             {
     73                 //写入文件
     74                 FILE *pfw = fopen(pathes[i], "w");
     75                 //处理前面n-1个
     76                 for (int j = 0; j < N / (num-1); j++)
     77                 {
     78                     char str[1024] = { 0 };
     79                     //读取一行写入一行
     80                     fgets(str, 1024, pf);
     81                     fputs(str, pfw);
     82                 }
     83                 fclose(pfw);
     84 
     85             }
     86 
     87             {
     88                 //处理剩下的
     89                 //写入
     90                 FILE *pfw = fopen(pathes[num-1], "w");
     91                 for (int j = 0; j < N %(num-1); j++)
     92                 {
     93                     char str[1024] = { 0 };
     94                     //读取一行写入一行
     95                     fgets(str, 1024, pf);
     96                     fputs(str, pfw);
     97                 }
     98                 fclose(pfw);
     99             }
    100         }
    101         fclose(pf);
    102     }
    103 }
    104 
    105 //合并文件
    106 void merge(char *newpath,int n)
    107 {
    108     char ** pathes = malloc(sizeof(char*)*n);
    109     for (int i = 0; i < n; i++)
    110     {
    111         pathes[i] = malloc(sizeof(char) * 256);
    112         sprintf(pathes[i], "dangdangwang%d.txt", i + 1);
    113     }
    114 
    115 
    116     //写入文件
    117     FILE *pf = fopen(newpath, "w");
    118     if (pf == NULL)
    119     {
    120         return -1;
    121     }
    122     else
    123     {
    124         //依次读取每个文件
    125         for (int i = 0; i < n;i++)
    126         {
    127             FILE *pfr = fopen(pathes[i], "r");
    128 
    129             while (!feof(pfr))
    130             {
    131                 char str[1024] = { 0 };
    132                 //读取一行写入一行
    133                 fgets(str, 1024, pfr);
    134                 fputs(str, pf);
    135             }
    136             fclose(pfr);
    137         }
    138         fclose(pf);
    139     }
    140 }
    141 
    142 
    143 void main()
    144 {
    145     //int num = getN(path);
    146     //printf("%d", num);获取行数
    147     int  num;
    148     scanf("%d", &num);
    149 
    150     //分割
    151     space(path, num);
    152     //合并
    153     merge(newpath, num);
    154 
    155     system("pause");
    156 }
  • 相关阅读:
    MySql常用日期时间查询
    微信开发中网页授权access_token与基础支持的access_token异同
    Sqlserver复杂查询
    Array排序和List排序
    关于java按位操作运算
    验证redis的快照和AOF
    线程安全 加锁机制
    Redis 与 数据库处理数据的两种模式
    redis实现点击量/浏览量
    jsp网站访问次数统计的几种方法
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8439622.html
Copyright © 2020-2023  润新知