63.当当网txt数据按行切割与合并
2024-08-31 16:14:03
- 获取文件有多少行
//获取文件有多少行
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");
}
最新文章
- obj转json
- October 3rd 2016 Week 41st Monday
- WPF WebBrowser屏蔽弹出alert ,confirm ,prompt ,showModalDialog() ,window.open()
- iOS开发笔记1:ToDoList、相册、地图应用及新浪微博
- iTerm和Alfred 2的安装和使用
- OpenGL&#174;ES基础
- Debian的一个命令
- 【POJ】【3071】Football
- Python爬虫预备知识
- poj 2051 Argus(优先队列)
- CF#213DIV2:B The Fibonacci Segment
- Knockout js 绑定 radio 时,当绑定整形的时候,绑定不生效
- 淘淘商城_day04_课堂笔记
- backup-mysql.sh
- 阿里云服务器解决mysql远程连接失败问题
- javascript学习日志:前言
- js监听事件
- Mongodb数据更新命令、操作符
- mac 下 clang++ 找不到头文件 stdlib.h
- redis加固
热门文章
- hadoop(八) - sqoop安装与使用
- vue20 父子组件数据交互
- Understanding IIS Bindings, Websites, Virtual Directories, and lastly Application Pools
- zzulioj--1815--easy problem(暴力加技巧)
- 35.QQ大数据模型
- 2015北京网络赛 J Scores bitset+分块
- Network Stack : Disk Cache
- Linux下解压缩文件命令总结
- python 协程学习
- XTUOJ 1206 Dormitory&#39;s Elevator