• 获取文件有多少行

     //获取文件有多少行
    int getN(char *path)
    {
    FILE *pf = fopen(path, "r");
    if (pf==NULL)
    {
    return -;
    }
    else
    {
    int i = ;
    while (!feof(pf))
    {
    char str[] = { };
    fgets(str, , pf);
    i++;
    }
    fclose(pf);
    return i;
    }
    }
  • 宏定义行数
     //数据的行数
    #define N 13180820
  • 文件按行切割
     //文件切割
    void space(char *path, int num)
    {
    char ** pathes = malloc(sizeof(char*)*num);
    for (int i = ; i < num;i++)
    {
    pathes[i] = malloc(sizeof(char) * );
    //格式化处理文件名
    sprintf(pathes[i], "dangdangwang%d.txt", i + );
    } //打开文件
    FILE *pf = fopen(path, "r"); if (pf == NULL)
    {
    return -;
    }
    else
    {
    //如果能被整除
    if (N%num == )
    {
    for (int i = ; i < num;i++)
    {
    //写入文件
    FILE *pfw = fopen(pathes[i], "w");
    for (int j = ; j < N / num; j++)
    {
    char str[] = { };
    //读取一行写入一行
    fgets(str, , pf);
    fputs(str, pfw);
    }
    fclose(pfw);
    }
    }
    else
    {
    for (int i = ; i < num - ; i++)
    {
    //写入文件
    FILE *pfw = fopen(pathes[i], "w");
    //处理前面n-1个
    for (int j = ; j < N / (num-); j++)
    {
    char str[] = { };
    //读取一行写入一行
    fgets(str, , pf);
    fputs(str, pfw);
    }
    fclose(pfw); } {
    //处理剩下的
    //写入
    FILE *pfw = fopen(pathes[num-], "w");
    for (int j = ; j < N %(num-); j++)
    {
    char str[] = { };
    //读取一行写入一行
    fgets(str, , pf);
    fputs(str, pfw);
    }
    fclose(pfw);
    }
    }
    fclose(pf);
    }
    }
  • 文件合并
     //合并文件
    void merge(char *newpath,int n)
    {
    char ** pathes = malloc(sizeof(char*)*n);
    for (int i = ; i < n; i++)
    {
    pathes[i] = malloc(sizeof(char) * );
    sprintf(pathes[i], "dangdangwang%d.txt", i + );
    } //写入文件
    FILE *pf = fopen(newpath, "w");
    if (pf == NULL)
    {
    return -;
    }
    else
    {
    //依次读取每个文件
    for (int i = ; i < n;i++)
    {
    FILE *pfr = fopen(pathes[i], "r"); while (!feof(pfr))
    {
    char str[] = { };
    //读取一行写入一行
    fgets(str, , pfr);
    fputs(str, pf);
    }
    fclose(pfr);
    }
    fclose(pf);
    }
    }
  • 测试函数
     void main()
    {
    //int num = getN(path);
    //printf("%d", num);获取行数
    int num;
    scanf("%d", &num); //分割
    space(path, num);
    //合并
    merge(newpath, num); system("pause");
    }

完整代码:

 #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
char *path = "dangdangwang.txt";
char *newpath = "dangdangwangN.txt"; //数据的行数
#define N 13180820 //获取文件有多少行
int getN(char *path)
{
FILE *pf = fopen(path, "r");
if (pf==NULL)
{
return -;
}
else
{
int i = ;
while (!feof(pf))
{
char str[] = { };
fgets(str, , pf);
i++;
}
fclose(pf);
return i;
}
} //文件切割
void space(char *path, int num)
{
char ** pathes = malloc(sizeof(char*)*num);
for (int i = ; i < num;i++)
{
pathes[i] = malloc(sizeof(char) * );
//格式化处理文件名
sprintf(pathes[i], "dangdangwang%d.txt", i + );
} //打开文件
FILE *pf = fopen(path, "r"); if (pf == NULL)
{
return -;
}
else
{
//如果能被整除
if (N%num == )
{
for (int i = ; i < num;i++)
{
//写入文件
FILE *pfw = fopen(pathes[i], "w");
for (int j = ; j < N / num; j++)
{
char str[] = { };
//读取一行写入一行
fgets(str, , pf);
fputs(str, pfw);
}
fclose(pfw);
}
}
else
{
for (int i = ; i < num - ; i++)
{
//写入文件
FILE *pfw = fopen(pathes[i], "w");
//处理前面n-1个
for (int j = ; j < N / (num-); j++)
{
char str[] = { };
//读取一行写入一行
fgets(str, , pf);
fputs(str, pfw);
}
fclose(pfw); } {
//处理剩下的
//写入
FILE *pfw = fopen(pathes[num-], "w");
for (int j = ; j < N %(num-); j++)
{
char str[] = { };
//读取一行写入一行
fgets(str, , pf);
fputs(str, pfw);
}
fclose(pfw);
}
}
fclose(pf);
}
} //合并文件
void merge(char *newpath,int n)
{
char ** pathes = malloc(sizeof(char*)*n);
for (int i = ; i < n; i++)
{
pathes[i] = malloc(sizeof(char) * );
sprintf(pathes[i], "dangdangwang%d.txt", i + );
} //写入文件
FILE *pf = fopen(newpath, "w");
if (pf == NULL)
{
return -;
}
else
{
//依次读取每个文件
for (int i = ; i < n;i++)
{
FILE *pfr = fopen(pathes[i], "r"); while (!feof(pfr))
{
char str[] = { };
//读取一行写入一行
fgets(str, , pfr);
fputs(str, pf);
}
fclose(pfr);
}
fclose(pf);
}
} void main()
{
//int num = getN(path);
//printf("%d", num);获取行数
int num;
scanf("%d", &num); //分割
space(path, num);
//合并
merge(newpath, num); system("pause");
}
05-27 18:45