Read binary file into struct, handle char [8], using C -
i have following code, prints out binary (prints first 4 characters (i'm guessing 2 bits per character, 8 chars in total?
void mychunks(){ struct chunkstorage { long sxid; char chunk[8]; // ‘chunk of data’ }; file *p; struct chunkstorage d; p=fopen("myfile.txt","rb"); while(1){ fread(&d.chunk,sizeof(d.chunk),1,p); if(feof(p)!=0) break; printf(“%s”, d.chunk); } fclose(p); } mychunks();
so way me able @ data, seems using while loop in way. i'm wondering how can concatenate this, , hex encode it?
what char chunk[8] do? why have loop on each 8 bytes? why cant forget "while" loops , read full chunk after fopen statement , convert full data hex?
is right concatenate, full file, or erroneous?
i not sure of history of file, or other parts of structure defined, strict c
standpoint, file large quantity of 8 byte values, nulls in them (or else printf
invalid.)
so yes, following possible if code valid:
typedef struct justchunk { char chunk[8] ; } chunker ; chunker * ptr ; ptr= (chunker * ) malloc( 1000000 * sizeof( chunker ) ) ; int iret ; f= fopen( "myfile.txt", "rb") ; iret= fread( ptr, sizeof( chunker), 1000000, f) ;
the value returned in iret
how many complete records read.
error handling, , file closing left exercise reader.
Comments
Post a Comment