博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux Linux程序练习二
阅读量:7025 次
发布时间:2019-06-28

本文共 1231 字,大约阅读时间需要 4 分钟。

/*编写一个程序读取a.txt文件,将文件内容数字从小到大排序,并将排序结果写入b.txt。 */#include 
#include
#include
#include
//插入排序void InertionSort(int * arr,int len){ if(arr==NULL) { printf("传入参数不可以为空!\n"); return; } int i=0,j=0,k=0,temp=0; for(i=1;i
=0&&temp
=arr[j]时,将不会进入循环,此时j是有序数组的最后一个元素 //arr[j]=temp;会将最后一个有序元素覆盖了 //arr[j]=temp; 错误 arr[k]=temp; }}int main(int arg, char * args[]){ if(arg<3) { printf("本程序需要两个参数!\n"); return 0; } //define file stream FILE *pfr=NULL; //open the file in read mode pfr=fopen(args[1],"r"); //judge if(pfr==NULL) { printf("read the file failed ! error msg:%s\n",strerror(errno)); return 0; } //create arr int index=0; //这里index是作为数组长度使用的,index的初始值是0,所以循环之后index会多1,正好此时index就是数组的长度 int arr[100]={ 0}; char buf[10]={ 0}; while(fscanf(pfr,"%s",buf)!=EOF) { arr[index++]=atoi(buf); memset(buf,0,sizeof(buf)); } //close the file stream if(pfr) { fclose(pfr); pfr=NULL; } InertionSort(arr,index); //define new file stream FILE * pfw=NULL; //open new file in append mode pfw=fopen(args[2],"a"); if(pfw==NULL) { printf("write the file failed ! error msg:%s\n",strerror(errno)); return 0; } //write the file int num=0; while(num

转载地址:http://jmoxl.baihongyu.com/

你可能感兴趣的文章
Maven阿里云高速镜像
查看>>
获取汉字首字母
查看>>
如何最快获取google ip 地址
查看>>
LoadRunner and Performance Center Blog
查看>>
c# 安装win服务
查看>>
SpringBoot整合swagger2
查看>>
IE6 css position:fixed无效的解决办法
查看>>
Android 4.4 WebView实现WebSocket即时通讯
查看>>
java线程池理解
查看>>
快速部署ceph集群
查看>>
vavr库
查看>>
一些更新android SDK 的ip地址
查看>>
python for CFD(第二步)
查看>>
python闭包
查看>>
netty源码分析之揭开reactor线程的面纱(一)
查看>>
【转】spring中props,list,set,map元素在配置文件中的用法
查看>>
java,hibernate和sql server对应的数据类型
查看>>
ArithmeticException问题解决
查看>>
Hibernate 一对多
查看>>
Struts2 标签库讲解
查看>>