Nitish Upreti
Nitish Upreti

Reputation: 6550

How to handle File Blocks in C?

I am beginner when it comes to C Programming language.

I want to read a file block of 1 KB in memory and store it in a In-Memory HashTable. To handle collision I have a doubly linked list for each bucket in my HashTable.

Suppose I model a node of my linked list as a structure with next and previous pointer. How should I be storing this file block of 1 KB?

Any pointers or ideas appreciated.

Upvotes: 0

Views: 105

Answers (1)

user1338
user1338

Reputation: 335

typedef struct struct_FILEBLOCK FILEBLOCK ;

struct struct_FILEBLOCK {
    char             *data ;
    FILEBLOCK        *next ;
    FILEBLOCK        *prev ;
    } ;

then all your actual data blocks can be exactly 1k long, and your list elements tiny.

FILEBLOCK *fballoc(void)
{
FILEBLOCK *fb = malloc(sizeof(FILEBLOCK)) ;
fb->data = malloc(1024) ;
fb->next = 0 ;
fb->prev = 0 ;
return fb ;
}

then

void fbread(int fh,FBLOCK *fb)
{
    read(fh,fb->data,1024) ;
    /* whateva */
    }

Upvotes: 1

Related Questions