Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12300048
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类:

2012-11-02 20:15:15

一、案例完整代码


点击(此处)折叠或打开

  1. /****************************************************************
  2. * Name : sort_and_output.c
  3. * Author : dyli2000
  4. * Date : 20121102
  5. * Description :
  6.     对学生成绩由高到低输出案例。扩展功能:
  7. 随机产生学生成绩,并写入到fin.txt;然后读fin.txt中的数据,排序输出到out.txt
  8. 文件。
  9. ****************************************************************/

  10. #include <stdio.h>
  11. #include <time.h>
  12. #include <string.h>

  13. #define DEBUG
  14. #define TIMES 20

  15. typedef struct StringNode
  16. {
  17.     char str[100];
  18.     int num;
  19. }str_node;

  20. /* Must put it here !! */
  21. str_node str_node_array[TIMES];
  22. int fsort(char* fin,char* fout)
  23. {
  24.     #ifdef DEBUG
  25.     printf("Before sort the string:\n%s\n",fin);
  26.     #endif

  27.     //str_node str_node_array[TIMES]; // Error usage
  28.     memset(str_node_array,0,TIMES);

  29.     int in = 0;
  30.     char *p[TIMES];
  31.     char *buf = fin;
  32.     char *p_num = NULL;
  33.     char *outer_ptr = NULL;
  34.     char *inner_ptr = NULL;

  35.     #ifdef DEBUG
  36.     printf("---------------------1111111111111111111111111111111111 buf:\n%s\n",buf);
  37.     #endif

  38.     while((p[in] = strtok_r(buf,"\n",&outer_ptr)) != NULL && in < TIMES)
  39.     {
  40.         buf = p[in];
  41.         strcpy(str_node_array[in].str,buf);

  42.         if( (strtok_r(buf," ",&inner_ptr) ) != NULL )
  43.         {
  44.             if( (p_num = strtok_r(NULL," ",&inner_ptr) ) != NULL)
  45.             {
  46.                 str_node_array[in].num = atoi(p_num);
  47.                 #ifdef DEBUG
  48.                 printf("str_node_array[%d].str:%s\n",in,str_node_array[in].str);
  49.                 printf("str_node_array[%d].num:%d\n",in,str_node_array[in].num);
  50.                 #endif
  51.             }
  52.         }

  53.         in++;
  54.         buf = NULL;
  55.     }
  56.     #ifdef DEBUG
  57.     printf("---------------------2222222222222222222222222222222222 str_node_array[0].str = %s\n",str_node_array[0].str);
  58.     #endif

  59.     //#if 0
  60.     int count = 0;
  61.     int j = 0;
  62.     str_node tmpnode;

  63.     for(; count < TIMES; count++)
  64.     {
  65.         /* upper bubble */
  66.         for(; j < TIMES - count; j++)
  67.         {
  68.             if(str_node_array[j].num < str_node_array[j+1].num)
  69.             {
  70.                 strcpy(tmpnode.str,str_node_array[j].str);
  71.                 tmpnode.num = str_node_array[j].num;

  72.                 strcpy(str_node_array[j].str,str_node_array[j+1].str);
  73.                 str_node_array[j].num = str_node_array[j+1].num;

  74.                 strcpy(str_node_array[j+1].str,tmpnode.str);
  75.                 str_node_array[j+1].num = tmpnode.num;
  76.             }
  77.         }
  78.         j = 0;
  79.     }
  80.     //#endif

  81.     #ifdef DEBUG
  82.     printf("---------------------3333333333333333333333333333333333\n");
  83.     #endif

  84.     j = 0;
  85.     int len = 0;
  86.     memset(fout,0,300);
  87.     for(; j < TIMES; j++)
  88.     {
  89.         // strcpy(fout+j,str_node_array[j].str); // The wrong
  90.         len += sprintf(fout+len,"%s \n",str_node_array[j].str);
  91.         printf("--------------------- %d,str_node_array[%d].str = %s,str_node_array[j].num = %d\n"
  92.                 ,j,j,str_node_array[j].str,str_node_array[j].num);
  93.     }
  94.     *(fout+len) = '\0';
  95.     printf("After sort the string\n%s\n",fout);

  96.     return 0;
  97. }

  98.  

  99. int main()
  100. {
  101.     FILE *fp;
  102.     char write_buf[100];
  103.     char read_buf[300];
  104.     char out_buf[300];
  105.     int count = 0;

  106.     memset(write_buf,0,100);
  107.     memset(read_buf,0,300);
  108.     memset(out_buf,0,300);

  109.     fp = fopen("in.txt","w+");
  110.     if(fp == NULL)
  111.     {
  112.         perror("fopen");
  113.         return;
  114.     }

  115.     /* Write to file */
  116.     srand( (unsigned int)time(0));
  117.     for(; count < TIMES; count++)
  118.     {
  119.         sprintf(write_buf,"Name %d \n",rand()%100);
  120.         fwrite(write_buf,1,strlen(write_buf),fp);
  121.     }
  122.     fseek(fp,0L,SEEK_END);
  123.     int buf_len = ftell(fp);
  124.     fseek(fp,0L,SEEK_SET); // <=> rewind(fp);

  125.     /* Read from file */
  126.     fread(read_buf,1,buf_len,fp);
  127.     read_buf[buf_len] = '\0';
  128.     #ifdef DEBUG
  129.     printf("%s",read_buf);
  130.     #endif

  131.     /* sort and output to out.txt */
  132.     #ifdef DEBUG
  133.     printf("----------------------------------------------\n");
  134.     #endif
  135.     fsort(read_buf,out_buf);

  136.     #ifdef DEBUG
  137.     printf("==============================================\n");
  138.     #endif

  139.     FILE *stream;
  140.     stream = fopen("out.txt","w+");
  141.     if(stream == NULL)
  142.     {
  143.         perror("fopen");
  144.         return;
  145.     }

  146.     fwrite(out_buf,1,strlen(out_buf),stream);

  147.     #ifdef DEBUG
  148.     printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
  149.     #endif

  150.     fclose(fp);
  151.     fclose(stream);
  152.     return 0;
  153. }

 

二、运行效果

image

图1 随机生成的in.txt文件


image

图2 排序后输出的out.txt文件

 

三、几个重要说明

要实现这个功能,涉及随机数据的生成、文件的读写、数据排序、字符串的分割与合并。下面这段代码为其中的一个关键。

实现数据分割的关键代码。


  1. while((p[in] = strtok_r(buf,"\n",&outer_ptr)) != NULL && in < TIMES)
  2.     {
  3.         buf = p[in];
  4.         strcpy(str_node_array[in].str,buf);

  5.         if( (strtok_r(buf," ",&inner_ptr) ) != NULL )
  6.         {
  7.             if( (p_num = strtok_r(NULL," ",&inner_ptr) ) != NULL)
  8.             {
  9.                 str_node_array[in].num = atoi(p_num);
  10.                 #ifdef DEBUG
  11.                 printf("str_node_array[%d].str:%s\n",in,str_node_array[in].str);
  12.                 printf("str_node_array[%d].num:%d\n",in,str_node_array[in].num);
  13.                 #endif
  14.             }
  15.         }

  16.         in++;
  17.         buf = NULL;
  18.     }

(1)、本例数据特点


  1. Name 96
  2. Name 93
  3. Name 83
  4. Name 81
  5. Name 74
  6. Name 70
  7. Name 66
  8. Name 55
  9. Name 54
  10. 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 :指向被分割成的前段字符串的指针。

阅读(3075) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~