La struttura necessaria alla lettura del file è questa:
codice:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* random rec_1ord description - could be anfiles_numthing */
struct rec_1
{
char num_files[2];
char files_num[2];
char zeroes[49];
};
struct rec_2to255 { /* description of data files */
unsigned char file_num; /* file #, i.e., F# */
char file_type[2]; /* CT file type = 0'e' (5 or 7 flds) */
unsigned char rec_len; /* record length in bytes (4 x num_fields)*/
unsigned char num_fields; /* number of 4-byte fields in each record*/
char reserved1[2]; /* in the data file */
char issue_name[16]; /* stock name */
char reserved2;
char CT_v2_8_flag; /* if CT ver. 2.8, 'Y'; o.w., anything else */
float first_date; /* yymmdd */
float last_date;
char time_frame; /* data format: 'I'(IDA)/'W'/'Q'/'D'/'M'/'Y' */
char ida_time[2]; /* intraday (IDA) time base */
char symbol[14]; /* stock symbol */
char reserved3; /* MetaStock reserved2: must be a space */
char flag; /* ' ' or '*' for autorun */
char reserved4;
};
che comi vedi include due campi di tipo float.
Per la lettura dal file scrivo:
codice:
int main()
{
int i,j,num1,num2;
FILE *f;
struct rec_1 r;
struct rec_2to255 r2;
f=fopen("Master_old","rb");
if (!f)
return 1;
num1=fread(&r,sizeof(struct rec_1),1,f);
long n = ftell(f);
printf("\nposizione %d \n\n",n);
printf("\nBytes letti %d \n\n",num1);
printf("\nnum_files : %d , file num : %d ,zeroes : %s \n\n",r.num_files,r.files_num,r.zeroes);
printf("###### Entro nel for ###### \n\n");
while((num2=fread(&r2,sizeof(struct rec_2to255),1,f)))
{
float b= atof(r2.first_date);
printf("PROVA %f \n", b);
printf("file_num : %d \n",r2.file_num);
printf("file_type : %d%c \n",r2.file_type[0],r2.file_type[1]);
printf("rec_len : %d \n",r2.rec_len);
printf("num_fields : %d \n",r2.num_fields);
printf("reserved1 : %d%d \n",r2.reserved1[0],r2.reserved1[1]);
printf("issue_name : %s \n",r2.issue_name);
printf("reserved2 : %d \n",r2.reserved2);
printf("CT_v2_8_flag : %d \n",r2.CT_v2_8_flag);
printf("first_date : %d|%d|%d|%d \n",r2.first_date[0],r2.first_date[1],r2.first_date[2],r2.first_date[3]);
printf("last_date : %d|%d|%d|%d \n",r2.last_date[0],r2.last_date[1],r2.last_date[2],r2.last_date[3]);
printf("time_frame : %c \n",r2.time_frame);
printf("ida_time : %d%d \n",r2.ida_time[0],r2.ida_time[1]);
printf("symbol[14] : %s \n",r2.symbol);
printf("reserved3 : %d \n",r2.reserved3);
printf("flag : %d \n",r2.flag);
printf("reserved4 : %d \n",r2.reserved4);
n = ftell(f);
printf("\nposizione %d \n\n",n);
printf("\nBytes letti %d \n\n",num2);
}
fclose(f);
printf("\n");
}
La lettura in questo modo batte pari solo fino alla lettura del primo dei due campi float... da lì in avanti inizia a sfasare tutto.
Il file tuttavia è corretto perchè se invece che dei float metto nella struttura dei char[4] la lettura avviene correttamente (ovviemente però poi il numero non è più leggibile...)