Thread: Converting types while keeping same size.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20

    Converting types while keeping same size.

    Hi all.
    I need urgent help. I have a deadline to meet and i am stuck on this stupid problem.
    Basically i am implementing my own virtual file system. I need to store data on it, raw data, about files and stuff. The problem is if i have a structure of 512 bytes, i need to store it on the virtual disk with size of 512 bytes. I can read from and write to the disk character buffers. I need to store the structure on the disk. But this structure has ints and char* s.
    If i convert the ints to chars i get larger overall buffer which can't fit in a 512 bytes on disk.

    So is there any way i can convert the struct to a char*, keeping the ints to 4 bytes characters. Actually they are unsigned shorts (2 bytes). So it's like i need to read 1 byte from the short and convert it to char and then read the other byte. (then convert back when filling the structure).

    Thanks alot, guys!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I hope your not trying to dump the struct straight to disk, since thats highly unportable (who's to say another system structures the struct the same in memory as your system does?)

    Create a function to dump the struct to disk, then create another one to read it:

    Code:
    typedef struct mystruct_t {
        char * blah;
        unsigned short int whoCares;
    } mystruct;
    
    void WriteToDisk(const char * f, const mystruct * ms)
    {
        FILE * theFile;
        /* open the file */
        fprintf("%s%d", ms->blah, ms->whoCares); /* look at the modifiers of printf(), use them to control the width of the strings & numbers you write */
        return;
    }
    
    void ReadFromDisk(const char * f, mystruct * ms)
    {
        /* cbf */
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But this structure has ints and char* s.
    Code:
    struct foo {
      int a;
      char *b;
    };
    Are you saying this (with lots of members) is 512 bytes?
    If it really does contain pointers, you can't write the whole thing in one step, and the result is almost certainly not going to be 512 bytes all the time.

    Or are you being lazy with your terminology, and you really have a char array in your struct
    Code:
    struct foo {
      int a;
      char b[510];
    };
    Are you saying this is 512 bytes?
    This you could write out to disk in one step, and it would always occupy 512 bytes.

    Post your actual struct, and we'll tell you the best way(s) to write it to disk.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Location
    Beirut, Lebanon
    Posts
    20
    Code:
    typedef struct fs_dir_entry_t {
    char name[14]; /* 13-character name */
    unsigned short id; /* Index of DCB or FCB */
    } dir_entry_t;
    Code:
    typedef struct fs_dcb_t { /* Directory Control Block */
    char marker; /* Directory or file marker */
    char num_entries; /* Number of entries in this DCB */
    unsigned short next; /* Index of next DCB in this dir */
    unsigned short prev; /* Index of prev DCB in this dir */
    unsigned short parent; /* Index of containing dir’s DCB */
    unsigned char padding[8]; /* Unused! */
    struct fs_dir_entry_t entries[31]; /* indices of dir/file blocks */
    } dir_block_t;
    These are the structures i was talking about. The second one is what i'm interested in, though it contains an array of the first one. So it's size is 512 bytes.

    The proposed one by zacs7 wouldn't work because i can only write a character buffer, which must be passed to a function that does the writing. And even if i somehow do that, the unsigned short will occupy more than 2 character bytes.

    Fortunately, i found a way to accomplish this. I created to functions, struct_to_string and string_to_struct that do the converting for me. The first just adds all the information to the buffer, but for shorts, it takes the 1st 8 bits and puts it in 1 char, and the next 8 bit in another. Using the binary operations i was able to keep the structure and string the same size. The second does the exact opposite.

    But thanks for the help anyway guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Keeping window open
    By jpipitone in forum C Programming
    Replies: 5
    Last Post: 12-01-2003, 12:56 PM