一個實用的從文本文件讀取數據進行排序的程序,文本文件排序
程序說明
這是一個十分可靠的程序,這個程序的查錯能力非常強悍。程序包含了文件操作,歸並排序和字符串輸入等多種技術。
程序的功能是從外部讀取一個包括int型數據的文本文件,然後將它保存到內部臨時數組,對數組進行排序後,以文本形式輸出到指定的文件上。因為是int類型的數據,沒有很嚴重的損失精度的問題。
正常運行要求:
包括數據的源文件內不能包括其他任何除數字和空白字符(空格,制表符,換行符)之外的任何字符,源文件最開始必須是數字字符,要保證源文件的數據計數正確。同時保證文件名有效。
完整代碼
警告:版權所有,謹供參考!
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012110062273.gif)
![]()
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/*=============================
制作於:Aug 16, 2016
by QQ:1729403632
===============================*/
#define ST 64 //字符串大小
void mergesort(int *, int);
void _mergesort(int *, int, int, int *);
void merge(int *, int, int, int, int *);
char * s_gets(char *, int);
int main(int argc, char * argv[]){
FILE * sor, * dest; //sor源文件 dest目標文件
int * ptr;//臨時數組
int i, n; //n表示元素個數
char fname[ST]; //臨時存儲字符串
printf("請輸入元素個數:");
while( scanf("%d", &n) != 1 || n <= 0 ){
printf("輸入錯誤,請重新輸入!\n");
while(getchar() != '\n')
continue;
}
while(getchar() != '\n')
continue;
ptr = (int *)malloc( (size_t)n * sizeof(int) ); //申請動態數組//////
if(ptr == NULL){
fprintf(stderr, "FAIL TO ASK FOR MEMORY SPACE\n");
exit(EXIT_FAILURE);
}
printf("請輸入原文件名:");
if( s_gets(fname, ST) == NULL ){
fprintf(stderr, "Fail to get a string\n");
exit(EXIT_FAILURE);
}
sor = fopen(fname, "r"); //打開包含數據的源文件
if(sor == NULL){
fprintf(stderr, "Fail to open the source file\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < n; i++) //獲取數據到動態數組
if( fscanf(sor, "%d", &ptr[i]) != 1 ){
fprintf(stderr, "Fail to get the data\n");
exit(EXIT_FAILURE);
}
mergesort(ptr, n); //排序
printf("請輸入要保存數據的文件名:");
if( s_gets(fname, ST) == NULL ){
fprintf(stderr, "Fail to get a string\n");
exit(EXIT_FAILURE);
}
dest = fopen(fname, "w"); //打開目標文件
if(dest == NULL){
fprintf(stderr, "Fail to open the destination file\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < n; i++){ //輸出數據到目標文件
if( fprintf(dest, "%d\t", ptr[i]) < 0 ){
fprintf(stderr, "Fail to save the data\n");
exit(EXIT_FAILURE);
}
if( ((i + 1) % 10) == 0){ //如果寫滿10個就換行
if( fprintf(dest, "\n") < 0 ){
fprintf(stderr, "Fail to save the data\n");
exit(EXIT_FAILURE);
}
}
}
if( fclose(sor) != 0 ){ //關閉源文件
fprintf(stderr, "Fail to close the source file\n");
exit(EXIT_FAILURE);
}
if( fclose(dest) != 0 ){ //關閉目標文件
fprintf(stderr, "Fail to close the destination file\n");
exit(EXIT_FAILURE);
}
free(ptr); //釋放內存
printf("成功完成!\n請按任意鍵繼續^ ^\b\b");
getch();
return 0;
}
void mergesort(int * ar, int size){
if(size > 0){
int * temp;
temp = (int *)malloc( (size_t)size * sizeof(int) ); /////
if(temp == NULL){
fprintf(stderr, "Fail to ask for MEMORY SPACE\n");
exit(EXIT_FAILURE);
}
_mergesort(ar, 0, size - 1, temp); //歸並排序
free(temp);
}
}
void _mergesort(int * ar, int start, int end, int * temp){
if(start < end){
int mid = (start + end) / 2;
_mergesort(ar, start, mid, temp); //左子數組排序
_mergesort(ar, mid + 1, end, temp); //右子數組排序
merge(ar, start, mid, end, temp); //合並子數組
}
}
void merge(int * ar, int p, int q, int r, int * temp){
int i = p, j = q + 1, k = 0;
while(i <= q && j <= r){
if(ar[i] < ar[j])
temp[k++] = ar[i++];
else
temp[k++] = ar[j++];
}
while(i <= q) //如果序列[i...q]存在,追加
temp[k++] = ar[i++];
while(j <= r) //如果序列[j...r]存在,追加
temp[k++] = ar[j++];
for(k = 0; k <= (r - p); k++)
ar[p + k] = temp[k];
}
char * s_gets(char * st, int size){
char * re;
int i = 0;
re = fgets(st, size, stdin);
if(re){
while(st[i] != '\n' && st[i] != '\0') //如果沒有到輸入字符串結束
i++; //遞增
if(st[i] == '\n') //如果字符串最後一個字符是'\n'
st[i] = '\0'; //把它變成'\0'
else //否則緩沖區內還有一部分超出讀取范圍的字符沒有被讀取
while(getchar() != '\n') //把這些字符讀取完(清空緩沖區)
continue;
}
return re;
}
main.c
運行結果
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012110062233.png)
data.txt:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012110062371.png)
obj.txt:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012110062363.png)