读取通达信的day日线文件,暂时只存了日期,开盘,收盘,最高,最低数值。
该文件由于不是全数据的,最开始的几个数据不知是什么,中间也含有大量的0的部分。需要处理跳过这些无用的区域。
实现的是从2012年开始,将所有数据存入一个单链表中。
- /*
- * =====================================================================================
- *
- * Filename: Read.c
- *
- * Description: read date
- *
- * Version: 1.0
- * Created: 07/13/2012 11:27:21 PM
- * Revision: none
- * Compiler: gcc
- *
- * Author: royn.wang.renyuan@gmail.com
- * Organization:
- *
- * =====================================================================================
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- typedef struct tagDayData{
- int date;
- float start;
- float end;
- float highest;
- float lowest;
- float volume;
- struct tagDayData* next;
- } *DayData, _DayData;
- void ReadTail(const char *path){
- //set today to the max time
- time_t rawtime;
- struct tm * hrtime;
- time(&rawtime);
- hrtime = localtime( &rawtime);
- long maxtime;
- maxtime = (hrtime->tm_year 1900)*(long)10000 (hrtime->tm_mon 1)*100 hrtime->tm_mday;
- //read data
- FILE* fid;
- unsigned int p;
- p = 0;
- fid=fopen(path,"r");
- //only the data in 2012 is valid;
- while(!feof(fid) &&( p>maxtime || p<20120000)){
- fread(&p, sizeof(unsigned int), 1, fid);
- }
- printf("%d \n",p);
- DayData head = (DayData) malloc(sizeof(_DayData));
- DayData ptr = head;
- printf("max = %ld \n", maxtime);
- while(p<maxtime && p >20120000){
- DayData cur = (DayData) malloc(sizeof(_DayData));
- ptr->next = cur;
- cur->date = p;
- printf("0: ptrdate = %d date = %d \n", ptr->date,cur->date);
- fread(&p, sizeof(unsigned int), 1, fid);
- cur->start = p/(float)100;
- fread(&p, sizeof(unsigned int), 1, fid);
- cur->highest = p/(float)100;
- printf("1:highest = %f \n", cur->highest);
- fread(&p, sizeof(unsigned int), 1, fid);
- cur->lowest = p/(float)100;
- fread(&p, sizeof(unsigned int), 1, fid);
- cur->end = p/(float)100;
- ptr = cur;
- fseek(fid, 3*sizeof(unsigned int), 1);
- //read the date of next day
- fread(&p, sizeof(unsigned int), 1, fid);
- printf("max = %ld..... p = %d \n", maxtime,p);
- }
- fclose(fid);
- }
- /*
- * === FUNCTION ======================================================================
- * Name: main
- * Description:
- * =====================================================================================
- */
- int
- main ( int argc, char *argv[] )
- {
-
- ReadTail("sh600242.day",0);
- return EXIT_SUCCESS;
- } /* ---------- end of function main ---------- */
阅读(1938) | 评论(0) | 转发(0) |