PDA

View Full Version : [C] Leggere da file binario


Manugal
07-04-2006, 16:17
Ciao a tutti! :)

Ho un problema nel leggere dei dati da un file binario. Praticamente devo leggere l'header del file che è una struttura (creata in funzioni precedenti per scriverla poi su file in modalità binaria) fatta in questo modo:

typedef struct{
char name[DBF_NAME_LENGTH+1];
short NumCampi;
long NumRecords;
short dimHeader;
short dimRecord;
long timestamp;
}header;


Il codice che ho scritto è il seguente:

/* verificaLettura mi dice se tutte le letture sono andate a buon fine. verificaSeek mi dice se tutti gli spostamenti nel file sono andati a buon fine. ihndl[handle] è un array di strutture che contiene, oltre ad altri campi, il File Pointer. */

verificaLettura+=fread(DBFInfo->Name,sizeof(char),DBF_NAME_LENGTH+1,ihndl[handle].fp);
verificaSeek+=fseek(ihndl[handle].fp,sizeof(char)*DBF_NAME_LENGTH+1,SEEK_CUR);
verificaLettura+=fread(&tmp1,sizeof(short),1,ihndl[handle].fp);
DBFInfo->NumFields=(uint16_t) ntohs(tmp1);
verificaSeek+=fseek(ihndl[handle].fp,sizeof(short),SEEK_CUR);
verificaLettura+=fread(&tmp2,sizeof(long),1,ihndl[handle].fp);
DBFInfo->NumRecs=(uint32_t) ntohl(tmp2);
verificaSeek+=fseek(ihndl[handle].fp,sizeof(long),SEEK_CUR);
verificaLettura+=fread(&tmp1,sizeof(short),1,ihndl[handle].fp);
DBFInfo->HeaderLen=(uint16_t) ntohs(tmp1);
verificaSeek+=fseek(ihndl[handle].fp,sizeof(short),SEEK_CUR);
verificaLettura+=fread(&tmp1,sizeof(short),1,ihndl[handle].fp);
DBFInfo->RecordLen=(uint16_t) ntohs(tmp1);
verificaSeek+=fseek(ihndl[handle].fp,sizeof(short),SEEK_CUR);
verificaLettura+=fread(&tmp2,sizeof(long),1,ihndl[handle].fp);
DBFInfo->LastUpdate=(uint32_t) ntohl(tmp2);
verificaSeek+=fseek(ihndl[handle].fp,sizeof(long),SEEK_CUR);


Il problema è che ho verificato, tramite una printf, che praticamente non scrive nulla in quella struttura DBFInfo. Come mai?

Grazie.

Manugal
07-04-2006, 19:27
Nessuno? :(

Ho provato anche a leggere con fread mettendo il valore letto su un array invece che su DBFInfo->Name ma è lo stesso non prende niente, mi stampa una stringa tutta vuota. :cry:

Manugal
07-04-2006, 19:34
Risolto!!! :D

Ho fatto un rewind prima della scrittura e ora prende tutto. Mah sempre più strano sto C :)