<b>一、案例完整代碼</b>
點選(此處)折疊或打開
/****************************************************************
* Name : sort_and_output.c
* Author : dyli2000
* Date : 20121102
* Description :
對學生成績由高到低輸出案例。擴充功能:
随機産生學生成績,并寫入到fin.txt;然後讀fin.txt中的資料,排序輸出到out.txt
檔案。
****************************************************************/
#include stdio.h>
#include time.h>
#include string.h>
#define DEBUG
#define TIMES 20
typedef struct StringNode
{
char str[100];
int num;
}str_node;
/* Must put it here !! */
str_node str_node_array[TIMES];
int fsort(char* fin,char* fout)
#ifdef DEBUG
printf("Before sort the string:\n%s\n",fin);
#endif
//str_node str_node_array[TIMES]; // Error usage
memset(str_node_array,0,TIMES);
int in = 0;
char *p[TIMES];
char *buf = fin;
char *p_num = NULL;
char *outer_ptr = NULL;
char *inner_ptr = NULL;
printf("---------------------1111111111111111111111111111111111 buf:\n%s\n",buf);
while((p[in] = strtok_r(buf,"\n",&outer_ptr)) != NULL && in TIMES)
{
buf = p[in];
strcpy(str_node_array[in].str,buf);
if( (strtok_r(buf," ",&inner_ptr) ) != NULL )
{
if( (p_num = strtok_r(NULL," ",&inner_ptr) ) != NULL)
{
str_node_array[in].num = atoi(p_num);
#ifdef DEBUG
printf("str_node_array[%d].str:%s\n",in,str_node_array[in].str);
printf("str_node_array[%d].num:%d\n",in,str_node_array[in].num);
#endif
}
}
in++;
buf = NULL;
}
printf("---------------------2222222222222222222222222222222222 str_node_array[0].str = %s\n",str_node_array[0].str);
//#if 0
int count = 0;
int j = 0;
str_node tmpnode;
for(; count TIMES; count++)
/* upper bubble */
for(; j TIMES - count; j++)
if(str_node_array[j].num str_node_array[j+1].num)
strcpy(tmpnode.str,str_node_array[j].str);
tmpnode.num = str_node_array[j].num;
strcpy(str_node_array[j].str,str_node_array[j+1].str);
str_node_array[j].num = str_node_array[j+1].num;
strcpy(str_node_array[j+1].str,tmpnode.str);
str_node_array[j+1].num = tmpnode.num;
j = 0;
//#endif
printf("---------------------3333333333333333333333333333333333\n");
j = 0;
int len = 0;
memset(fout,0,300);
for(; j TIMES; j++)
// strcpy(fout+j,str_node_array[j].str); // The wrong
len += sprintf(fout+len,"%s \n",str_node_array[j].str);
printf("--------------------- %d,str_node_array[%d].str = %s,str_node_array[j].num = %d\n"
,j,j,str_node_array[j].str,str_node_array[j].num);
*(fout+len) = '\0';
printf("After sort the string\n%s\n",fout);
return 0;
}
int main()
FILE *fp;
char write_buf[100];
char read_buf[300];
char out_buf[300];
memset(write_buf,0,100);
memset(read_buf,0,300);
memset(out_buf,0,300);
fp = fopen("in.txt","w+");
if(fp == NULL)
perror("fopen");
return;
/* Write to file */
srand( (unsigned int)time(0));
sprintf(write_buf,"Name %d \n",rand()%100);
fwrite(write_buf,1,strlen(write_buf),fp);
fseek(fp,0L,SEEK_END);
int buf_len = ftell(fp);
fseek(fp,0L,SEEK_SET); // => rewind(fp);
/* Read from file */
fread(read_buf,1,buf_len,fp);
read_buf[buf_len] = '\0';
printf("%s",read_buf);
/* sort and output to out.txt */
printf("----------------------------------------------\n");
fsort(read_buf,out_buf);
printf("==============================================\n");
FILE *stream;
stream = fopen("out.txt","w+");
if(stream == NULL)
fwrite(out_buf,1,strlen(out_buf),stream);
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
fclose(fp);
fclose(stream);
<b>二、運作效果</b>

圖1 随機生成的in.txt檔案
圖2 排序後輸出的out.txt檔案
<b>三、幾個重要說明</b>
要實作這個功能,涉及随機資料的生成、檔案的讀寫、資料排序、字元串的分割與合并。下面這段代碼為其中的一個關鍵。 實作資料分割的關鍵代碼。
while((p[in] = strtok_r(buf,"\n",&outer_ptr)) != NULL && in TIMES)
(1)、本例資料特點
Name 96
Name 93
Name 83
Name 81
Name 74
Name 70
Name 66
Name 55
Name 54
Name 52
從in.txt讀出來的字元串,先要根據’\n’進行行分割,再根據’ ’進行行中分割。這裡使用普通的strtok函數是實作不了的。參考另外一篇博文《strtok的缺陷》。
(2)、strtok_r()函數
strtok_r()是系統專門為解決這個問題而設計的函數。參數清單:
p_str = strtok_r(buf,"\n",&outer_ptr))
buf : 要分割的字元串指針;
“\n”: 分割符;
&outer_ptr : 指向被分割成的後段字元串的指針的位址。
p_str :指向被分割成的前段字元串的指針。