用C語言實現(xiàn)從文本文件中讀取數(shù)據(jù)后進行排序的功能
功能介紹
程序的功能是從外部讀取一個包括int型數(shù)據(jù)的文本文件,然后將它保存到內(nèi)部臨時數(shù)組,對數(shù)組進行排序后,以文本形式輸出到指定的文件上。因為是int類型的數(shù)據(jù),沒有很嚴(yán)重的損失精度的問題。
正常運行要求:
包括數(shù)據(jù)的源文件內(nèi)不能包括其他任何除數(shù)字和空白字符(空格,制表符,換行符)之外的任何字符,源文件最開始必須是數(shù)字字符,要保證源文件的數(shù)據(jù)計數(shù)正確。同時保證文件名有效。
運行結(jié)果

data.txt:

obj.txt:

完整代碼
警告:版權(quán)所有,謹(jǐn)供參考!
#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目標(biāo)文件
int * ptr;//臨時數(shù)組
int i, n; //n表示元素個數(shù)
char fname[ST]; //臨時存儲字符串
printf("請輸入元素個數(shù):");
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) ); //申請動態(tài)數(shù)組//////
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"); //打開包含數(shù)據(jù)的源文件
if(sor == NULL){
fprintf(stderr, "Fail to open the source file\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < n; i++) //獲取數(shù)據(jù)到動態(tài)數(shù)組
if( fscanf(sor, "%d", &ptr[i]) != 1 ){
fprintf(stderr, "Fail to get the data\n");
exit(EXIT_FAILURE);
}
mergesort(ptr, n); //排序
printf("請輸入要保存數(shù)據(jù)的文件名:");
if( s_gets(fname, ST) == NULL ){
fprintf(stderr, "Fail to get a string\n");
exit(EXIT_FAILURE);
}
dest = fopen(fname, "w"); //打開目標(biāo)文件
if(dest == NULL){
fprintf(stderr, "Fail to open the destination file\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < n; i++){ //輸出數(shù)據(jù)到目標(biāo)文件
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 ){ //關(guān)閉源文件
fprintf(stderr, "Fail to close the source file\n");
exit(EXIT_FAILURE);
}
if( fclose(dest) != 0 ){ //關(guān)閉目標(biāo)文件
fprintf(stderr, "Fail to close the destination file\n");
exit(EXIT_FAILURE);
}
free(ptr); //釋放內(nèi)存
printf("成功完成!\n請按任意鍵繼續(xù)^ ^\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); //左子數(shù)組排序
_mergesort(ar, mid + 1, end, temp); //右子數(shù)組排序
merge(ar, start, mid, end, temp); //合并子數(shù)組
}
}
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') //如果沒有到輸入字符串結(jié)束
i++; //遞增
if(st[i] == '\n') //如果字符串最后一個字符是'\n'
st[i] = '\0'; //把它變成'\0'
else //否則緩沖區(qū)內(nèi)還有一部分超出讀取范圍的字符沒有被讀取
while(getchar() != '\n') //把這些字符讀取完(清空緩沖區(qū))
continue;
}
return re;
}
總結(jié)
以上就是用C語言實現(xiàn)從文本文件中讀取數(shù)據(jù)后進行排序功能的全部內(nèi)容,閱讀這篇文章后,大家自己進行調(diào)試運行,相信會對于學(xué)習(xí)C語言的朋友們很有幫助的。
相關(guān)文章
深入解析C++的循環(huán)鏈表與雙向鏈表設(shè)計的API實現(xiàn)
這篇文章主要介紹了C++的循環(huán)鏈表與雙向鏈表設(shè)計的API實現(xiàn),文中的示例對于鏈表結(jié)點的操作起到了很好的說明作用,需要的朋友可以參考下2016-03-03
C++ EasyX學(xué)習(xí)之鼠標(biāo)操作詳解
EasyX是針對C/C++的圖形庫,可以幫助使用C/C++語言的程序員快速上手圖形和游戲編程。本文將為大家詳細(xì)講講EasyX的鼠標(biāo)操作,需要的可以參考一下2022-07-07

