64,682
社区成员
发帖
与我相关
我的任务
分享
// Trade data has the following format:
// Name | Offset | Num Bytes | Type | Description
// -------------+--------+-----------+---------------+-----------------------------------------------------------
// Length | 0 | 2 | Integer | Length of the entire package (Including this data member).
// Type | 2 | 1 | Alpha | "T"
// Symbol | 3 | 5 | Alpha | Left-justified string with spaces filling the unused bytes
// Size | 8 | 2 | Integer | Item size
// Price | 10 | 8 | Floating Pt. | Item price
// Dynamic Data | 18 | | Dynamic Alpha | Reserved data; can be ignored.
#include <stdio.h>
#include <string.h>
#define SWAP(a,b) (a)^=(b)^=(a)^=(b)
FILE *f;
int i,n,r,DL;
char head[18];
unsigned short Length;
char Type;
char Symbol[6];
unsigned short Size;
double Price;
char Dynamic_Data[65518];
void main() {
f=fopen("data.bin","rb");
if (NULL==f) {
printf("Can not open file data.bin!\n");
return;
}
n=0;
while (1) {
r=fread(head,1,18,f);
if (r<18) break;
SWAP(head[0],head[1]);
Length=*((unsigned short *)head);
Type=head[2];
if ('T'!=Type) break;
strncpy(Symbol,head+3,5);Symbol[5]=0;
SWAP(head[8],head[9]);
Size=*((unsigned short *)(head+8));
for (i=0;i<4;i++) SWAP(head[10+i],head[17-i]);
Price=*((double *)(head+10));
DL=Length-18;
if (DL>0) {
r=fread(Dynamic_Data,1,DL,f);
if (r<DL) break;
}
n++;
printf("Pack %06d: Length=%hd,Type=%c,Symbol=%s,Size=%hd,Price=%lg,Dynamic Data Length=%d\n",n,Length,Type,Symbol,Size,Price,DL);
}
fclose(f);
}