Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1004092
  • 博文数量: 150
  • 博客积分: 3017
  • 博客等级: 少校
  • 技术积分: 3829
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-19 14:40
个人简介

Now in Baidu WISE team

文章分类

全部博文(150)

文章存档

2014年(8)

2013年(31)

2012年(111)

分类:

2012-08-08 23:49:14

读取通达信的day日线文件,暂时只存了日期,开盘,收盘,最高,最低数值。
该文件由于不是全数据的,最开始的几个数据不知是什么,中间也含有大量的0的部分。需要处理跳过这些无用的区域。
实现的是从2012年开始,将所有数据存入一个单链表中。

点击(此处)折叠或打开

  1. /*
  2.  * =====================================================================================
  3.  *
  4.  * Filename: Read.c
  5.  *
  6.  * Description: read date
  7.  *
  8.  * Version: 1.0
  9.  * Created: 07/13/2012 11:27:21 PM
  10.  * Revision: none
  11.  * Compiler: gcc
  12.  *
  13.  * Author: royn.wang.renyuan@gmail.com
  14.  * Organization:
  15.  *
  16.  * =====================================================================================
  17.  */

  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <time.h>


  21. typedef struct tagDayData{
  22.     int date;
  23.     float start;
  24.     float end;
  25.     float highest;
  26.     float lowest;
  27.     float volume;
  28.     struct tagDayData* next;
  29. } *DayData, _DayData;


  30. void ReadTail(const char *path){
  31.     //set today to the max time
  32.     time_t rawtime;
  33.     struct tm *    hrtime;
  34.     time(&rawtime);
  35.     hrtime = localtime( &rawtime);

  36.     long maxtime;
  37.     maxtime = (hrtime->tm_year 1900)*(long)10000 (hrtime->tm_mon 1)*100 hrtime->tm_mday;

  38.     //read data
  39.     FILE* fid;
  40.     unsigned int p;
  41.     p = 0;
  42.     fid=fopen(path,"r");
  43.     //only the data in 2012 is valid;

  44.     while(!feof(fid) &&( p>maxtime || p<20120000)){
  45.         fread(&p, sizeof(unsigned int), 1, fid);
  46.     }

  47.     printf("%d \n",p);

  48.     DayData head = (DayData) malloc(sizeof(_DayData));
  49.     DayData ptr = head;

  50.     printf("max = %ld \n", maxtime);
  51.     while(p<maxtime && p >20120000){
  52.         DayData cur = (DayData) malloc(sizeof(_DayData));
  53.         ptr->next = cur;
  54.         cur->date = p;
  55.         printf("0: ptrdate = %d date = %d \n", ptr->date,cur->date);
  56.         fread(&p, sizeof(unsigned int), 1, fid);
  57.         cur->start = p/(float)100;

  58.         fread(&p, sizeof(unsigned int), 1, fid);
  59.         cur->highest = p/(float)100;

  60.         printf("1:highest = %f \n", cur->highest);
  61.         fread(&p, sizeof(unsigned int), 1, fid);
  62.         cur->lowest = p/(float)100;

  63.         fread(&p, sizeof(unsigned int), 1, fid);
  64.         cur->end = p/(float)100;

  65.         ptr = cur;
  66.         fseek(fid, 3*sizeof(unsigned int), 1);

  67.         //read the date of next day
  68.         fread(&p, sizeof(unsigned int), 1, fid);
  69.         printf("max = %ld..... p = %d \n", maxtime,p);
  70.     }
  71.     fclose(fid);

  72. }



  73. /*
  74.  * === FUNCTION ======================================================================
  75.  * Name: main
  76.  * Description:
  77.  * =====================================================================================
  78.  */
  79.     int
  80. main ( int argc, char *argv[] )
  81. {
  82.     
  83.     ReadTail("sh600242.day",0);
  84.     return EXIT_SUCCESS;
  85. }                /* ---------- end of function main ---------- */

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