文件外部排序
2015-09-07 11:58
309 查看
问题摘要:
给外部文件里面没排好序的内容排序并将排好序的文件写入文件
例如:
没排序前:
排序后:
注意:我把文件是放在d:\数据.txt下面的,如果自己测试,要么更改路径,要么就要自己把要测试的文件放入该路径下。
思路很简单,就是将文件读进内存,然后排个序再写回去就行了。
给外部文件里面没排好序的内容排序并将排好序的文件写入文件
例如:
没排序前:
排序后:
注意:我把文件是放在d:\数据.txt下面的,如果自己测试,要么更改路径,要么就要自己把要测试的文件放入该路径下。
思路很简单,就是将文件读进内存,然后排个序再写回去就行了。
#include <iostream> #include <fstream> using namespace std; int num[100]; int select_sort(int num[],int count){//选择排序,这里只能排整型数组,如果要排别的类型,需要更改 for(int i=0;i<count;i++){ for(int j=i+1;j<count;j++){ if(num[j]<num[i]){ int temp = num[i]; num[i] = num[j]; num[j] = temp; } } } //写入文件 fstream f("d:数据.txt",ios::out); if(!f) cout<<"mistake"; for(i=0;i<count;i++) f<<num[i]<<" "; f.close(); return 0; } void main() { int count=0; fstream f("d:\\数据.txt",ios::in); if(!f) cout<<"mistake"; while(f>>num[count]&&!f.eof()){ //把文件读进内存,这个写的比较牛逼 count++; } f.close(); select_sort(num,count); }
相关文章推荐
- MAC下安装多版本JDK和切换几种方式
- 配置ADT
- matlab操作之--读取指定文件夹下的“指定格式”文件
- NodeJs http.get 方法请求时间过长问题处理!
- 每天进步一点点————磁盘I/O问题
- 事件 ID: 10016 解决方案
- jquery实现标题字体变换的滑动门菜单效果
- 苹果iPhone 6S将逼死iPad Mini
- 在eclipse中如何查找指定文文件
- Light oj 1025 (区间dp)
- IDEA+jetty配置jndi数据源
- R语言入门学习
- Scala总结
- SQL Server 删除作业Job (亲自实践)
- 办公室英语1
- warning : json_decode(): option JSON_BIGINT_AS_STRING not implemented in xxx
- 架构之路(二):性能
- 解决:Jersey搭建webService报错:com.sun.jersey.spi.inject.Errors$ErrorMessagesException
- YII 1.0 缓存使用
- 在Debian 8纯命令行安装android开发环境方法